Return to Snippet

Revision: 57821
at June 11, 2012 17:29 by BlackSheep


Initial Code
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>";
	}
?>

Initial URL
http://css-tricks.com/dynamic-dropdowns/

Initial Description
How to dynamically generate combo box contents depending on the selection made by a user on earlier combo box.

Initial Title
jQuery Dynamic Combobox

Initial Tags
jquery

Initial Language
jQuery