hosker Posted September 27, 2011 Share Posted September 27, 2011 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>";?> Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/ Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/#findComment-1273217 Share on other sites More sharing options...
hosker Posted September 27, 2011 Author Share Posted September 27, 2011 I don't want to populate another select list. I want to pull data from the database based upon the selection from the list. I basically want to set the variable $tournament from the form when i select it without creating a new page. Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/#findComment-1273223 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/#findComment-1273224 Share on other sites More sharing options...
hosker Posted September 27, 2011 Author Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/#findComment-1273228 Share on other sites More sharing options...
Buddski Posted September 27, 2011 Share Posted September 27, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247947-auto-populate-data-from-a-form-selection/#findComment-1273230 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.