Forum Moderators: open
Step 2: as you said - using the SELECT box onChange handler. The following should get you started (or convince you to give up ;)):
<SCRIPT type="text/javascript">
function Person (...) {
}
var peopleArray = new Array (
new Person ('firstname', 'lastname', '', '', ''),
new Person ('firstname', 'lastname', '', '', ''),
new Person ('firstname', 'lastname', '', '', ''),
...
)
function showPerson(form, index) {
form.firstname.value=peopleArray[index].firstname;
...
}
</SCRIPT>
<FORM ...>
<SELECT ... onChange="showPerson(this.form, selectedIndex);">
</SELECT>
<INPUT type="text" name="firstname" value="">
</FORM>
Shall i give a nice example for what i expect
Follow this link (Hotmail new sign up form).
Here the country has a combobox. When the user selects the country, the page gets submitted and fetches the corresponding states to another combo. How is it done? Also i need to retain the values so far entered in the page, even after the page appears after getting submitted..
If you want the page to reload and then display the values in the form then you only need JavaScript for the reload:
<SELECT name="personid" ... onChange="this.form.submit();">
<OPTION value="">-- select Person --</OPTION>
<OPTION value="id1">Person 1</OPTION>
<OPTION value="id1">Person 1</OPTION>
<OPTION value="id1">Person 1</OPTION>
</SELECT>
Then use PHP to load the values you want into the form after the reload.
I have some more doubts. Plz take a moment if you don't mind.
As you said i'm submitting the form as soon as the item changes. In the action of the form i'm writing the name of the php file in which the code forloading is written like
File - index.php
<FORM NAME="combo" METHOD="POST" ACTION="fetch.php">
<SELECT name="personid" ... onChange="this.form.submit();">
<OPTION value="">-- select Person --</OPTION>
<OPTION value="id1">Person 1</OPTION>
<OPTION value="id1">Person 1</OPTION>
<OPTION value="id1">Person 1</OPTION>
</SELECT>
</FORM>
I need to fetch the data from database corresponding to the item currently selected in combobox. How can i pass the index of that item to php page?
Also after fetching the data using fetch.php i need to display the contents in index.php. I need to set the item selected by user to be selected in the first combo and the corresponding data to be filled in the textboxex of index.php how can i transfer that data back to javascript in index.php from fetch.php?
Calling form.submit() then passes a GET or POST parameter "personid" which you can access in various ways using PHP.
In the loop that builds the SELECT list you can add something like:
<OPTION value="..."<?PHP if ($id == $personid) echo " selected";?>">...</OPTION>
Similarly, you need to run a query to select ALL the information for 'personid' and populate the form by setting the "value"s for each field.
OR, you could have just the SELECT list on page1.html then direct the form to page2.html where the rest of the information is displayed.