Forum Moderators: open

Message Too Old, No Replies

Javascript and PHP

problem with data transfer

         

Riab

8:01 am on Feb 23, 2004 (gmt 0)

10+ Year Member



I have a page with a combo box containing the names of some people. In the same page there are some textboxes in which the data like address, phone no, e-mail id etc of the people is to be displayed.
My requirement is like when an user selects a name in the combo box the corresponding information of the name selected should get filled in the textboxes in the same page. It should happen as soon as the item in the combo box is changed ie, using the OnChange fn. of combo box.
Please help me with a simple code snippet as i'm a beginner in web programming. I'm using PHP for server and MySQL as database.

dcrombie

10:49 am on Feb 23, 2004 (gmt 0)



Step 1: your page has to load ALL of the details using JavaScript - ie. use PHP to generate JavaScript that populates an array of people.

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>

Riab

5:58 pm on Feb 23, 2004 (gmt 0)

10+ Year Member



Hey you know, there is a bulk data for me to handle. So when i tried ur approach, the site took almost 5 min to download. So i can't adopt that.

Shall i give a nice example for what i expect
Follow this link (Hotmail new sign up form).

[registernet.passport.net...]

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..

dcrombie

6:15 pm on Feb 23, 2004 (gmt 0)



If you want the data to load immediately then you have to use JavaScript and you have to follow the method I outlined above.

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.

Riab

12:58 am on Feb 24, 2004 (gmt 0)

10+ Year Member



Thanx a lot for your help. you seem to be a veteran in this..

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?

dcrombie

10:27 am on Feb 24, 2004 (gmt 0)



You need to change your action to "index.php" or "<?= $PHP_SELF?>" so that it directs to the same page.

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.

Riab

3:05 am on Feb 25, 2004 (gmt 0)

10+ Year Member



Thanks a lot

But can u be a bit more descriptive.. I cannot understand clearly..

I'm a beginner in this field. my expertise is in c, java and VB.

dcrombie

11:56 am on Feb 25, 2004 (gmt 0)



You need to do all the above in a single page. Using conditional statements (ie. if) you can detect whether a 'person' has been selected (the FORM will pass the id as a variable) and display the additional information accordingly.

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.

ucbones

6:17 pm on Feb 26, 2004 (gmt 0)

10+ Year Member



ooh... be careful with your PHP, it by default has register globals off, hence $personid won't work anymore, you need to use $_POST["personid"].

ucbones