I have got 2 drop down list in my php form. Values in the drop down list would be retrieved from the database(mysql). I have got the first drop down list running. But now I need to update the 2nd drop down list upon the user’s selection. The value displayed in the 2nd drop down would defer depending on the user’s selection.
How do I update values of another drop down list after a user has selected an option from the first drop down list? I have searched and seen similar question on SO but they aren’t answered. Hope to get some help here. Thanks. Below is my current code I have.
PS. If I were to create a html form, I would need to run the php script upon lauching, without any inputs to populate the first drop down list. How do I so? I’m only aware of how to send request upon inputs; E.g. <input type=submit>
.php
<?php
$conn = mysql_connect("127.0.0.1","root","")
mysql_select_db(MyApp,$conn);
$sql2=("SELECT App.AppName FROM App);
$result2=mysql_query($sql2);
echo '<select name="sublist">';
echo '<option value=""></option>';
while ($row2 = mysql_fetch_assoc($result2))
{
echo '<option value="' . $row2['AppName'] . '">' . $row2['AppName']. '</option>';
}
echo '</select>';
?>
You have two solutions for this.
However to perform both things in good manner you need support of Javascript
So if I explain you the solution one. Once user change your first dropdown list. You have to POST your page to the same page using
onChange()event. Then in your page set the elements for the second dropdown list same as you did for the first dropdown.The second solution require AJAX. Once user change the first dropdown you make a AJAX request to a page. In that page you catch the selection user did. And you generate the second dropdown based on that. Here you can use three methods to get the second dropdown or it’s data.
a. You generate the all the options from that page and send back. Set the innerHTML of the second dropdown.
b. Get the data as JSON and populate your dropdown based on that.
c. Get AJAX XML and populate your dropdown based on that.