Server Side Cascading Dropdown In Php
Solution 1:
If you want this to be dynamic (i.e. after the user changes the dropdown) you will have to use javascript to firstly query a PHP page (probably using jQuery get) then adjust the dropdowns accordingly. There are lots of tutorials for this on the web.
If you just want the initial page data to be populated you can pick the first city from your query and set the option as selected, then use that city in your next query.
Something like:
$first = True;
while($row = mysql_fetch_array($result))
{
echo"<option" . (($first) ? " selected" : "") . ">" . $row['city'] . "</option>";
if($first)
{
$first = !$first;
$city = $row['city'];
}
}
//now do stuff with with $city
Solution 2:
<?$CITIES = query_result("SELECT DISTINCT cities FROM nights;");
if (isset($_POST["city"])) {
$NAMES = query_result("SELECT name FROM nights WHERE city = '{$_POST[city]}'");
if (isset($_POST["day"])
$DAYS = query_result("SELECT day FROM nights WHERE city='{$_POST[city]}'".
" AND name = '{$_POST[name]}'");
else$DAYS = array();
} else$NAMES = array();
/* now output the <select> markup for $CITIES, $NAMES and $DAYS */?>
Note: you have to define the query_result
function yourself (I just used it to simplify the code).
Solution 3:
You can use AJAX for this.
On change event call javascript-ajax function that call php script which makes you second dropdown caode.
Solution 4:
You will need to use ajax to load options from php script. Here is an example of how you can do it with jquery and ajax: Autopopulate Select Dropdown Box Using jQuery
Post a Comment for "Server Side Cascading Dropdown In Php"