Jump to content

Auto Populate Data from a form selection


hosker

Recommended Posts

I have a form which creates a drop down list from data in a MySQL database. I would like to be able to have data from my database automatically populate based upon the selection from the list. How can I do that? Javascript? AJAX? Any help would be appreciated.

 

 

<?php	$query2="SELECT tournament,id FROM 2011_Tournament";
								$result2=mysql_query($query2);
								echo "<select name=2011_Tournament value='tournament'>Tournament</option>";
								while($tournament=mysql_fetch_array($result2)){
								echo "<option value='$tournament[tournament]'>$tournament[tournament]</option>";
								}
								echo "</select>";?>

Link to comment
Share on other sites

From the script provided you want to populate/create another select element based on the selection of the "tournament"?

If so, use an onchange event on the tournament select to call an Ajax function based on the selected ID of the tournament box.

I would change the existing code to this:

$query2="SELECT tournament,id FROM 2011_Tournament";
$result2=mysql_query($query2);
echo '<strong>Tournament:</strong> <select name="2011_Tournament" onchange="populateList(this)">';
while($tournament=mysql_fetch_array($result2)) {
   echo '<option value="'.$tournament['id'].'">'.$tournament['tournament'].'</option>';
}
echo "</select>";

The populateList function will get the value of the selected object, post it to an ajax page which will return a select list for you to create.

Link to comment
Share on other sites

All you have to do is return the appropriate information from the AJAX and display it on your page.

The popluateList function (as I described earlier) can do whatever you want it to do, create new input fields, display textual information etc etc.

Can you give us an example of the information you wish to return from the selected element and what you want to do with it?

Link to comment
Share on other sites

From the drop down list, upon selection of said Golf Tournament, I would like all the picks made for that tournament:

 

 

                ID        Tournement                                                            User                                  Weekly_Pick

12 Arnold Palmer Invitational presented by MasterCard Hosker                         J.B. Holmes

12 Arnold Palmer Invitational presented by MasterCard PJPoker                         Jim Furyk

12 Arnold Palmer Invitational presented by MasterCard John Daly Look A Like Jim Furyk

12 Arnold Palmer Invitational presented by MasterCard Scratch Shooter          Ernie Els

12 Arnold Palmer Invitational presented by MasterCard Jay1232007                 Camilo Villegas

12 Arnold Palmer Invitational presented by MasterCard Jay Kennedy                 Nick Watney

12 Arnold Palmer Invitational presented by MasterCard Shane                         Robert Allenby

 

 

The above is data from my mySQL table. When I select the tournament from the drop down list, I would like to display the User and Weekly_Pick. I just need to set the variable tournament and I think that I can write the rest of the code myself.

Link to comment
Share on other sites

AJAX cannot set a PHP variable, it can only (in your case) be used to return information.

Your best bet would be to use AJAX to send the selected ID off to a PHP script that will generate the information that you require.

Once the information is output (from the AJAX called PHP script), the AJAX script will return the resulting data which you can display on your page.

 

Basic flow:

1. User selects a tournament.

2. AJAX posts the selected data to a PHP page.

3. PHP page fetches all the information and formats it all pretty looking.

4. AJAX returns the pretty information back to the original page.

5. Javascript then decides where this information is to be displayed.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.