/ Published in: jQuery
How to dynamically generate combo box contents depending on the selection made by a user on earlier combo box.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
HTML ==== <select id="first-choice"> <option selected value="base">Please Select</option> <option value="beverages">Beverages</option> <option value="snacks">Snacks</option> </select> <br /> <select id="second-choice"> <option>Please choose from above</option> </select> JS == $("#first-choice").change(function() { $("#second-choice").load("getter.php?choice=" + $("#first-choice").val()); }); PHP === <?php $username = "username"; $password = "password"; $hostname = "localhost"; $dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL"); $selected = mysql_select_db("dropdownvalues", $dbhandle) or die("Could not select examples"); $choice = mysql_real_escape_string($_GET['choice']); $query = "SELECT * FROM dd_vals WHERE category='$choice'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)) { echo "<option>" . $row{'dd_val'} . "</option>"; } ?>
URL: http://css-tricks.com/dynamic-dropdowns/