Kobi Posted March 1, 2013 Share Posted March 1, 2013 Hi, I want to make a dynamic dropdown menu for my site, i'm using php & mysql. I can fetch data from the database but something is not working. here is my code: <?php $city_set = " SELECT * FROM city"; $query_city = mysql_query($city_set); while($array_city[] = $city_set = mysql_fetch_object($query_city)); array_pop($array_city); ?> <div id="searchBoxColumn"> <h4>Choose Region</h4> <select name="Region"> <?php foreach($array as $option) : ?> <option value="<?php echo $option->ID; ?>"><?php echo $option->Region ?></option> <?php endforeach; ?> </select> <h4>Choose City</h4> <select name="city"> <?php foreach($array_city as $option) : ?> <option value="<?php echo $option->ID; ?>"><?php echo $option->city ?></option> <?php endforeach; ?> </select> <h4>Choose Category</h4> <select name="category"> <script language="javascript"> function setOptions(chosen) { var selbox = document.myform.selectmodel; selbox.options.length = 0; if (chosen == "0") { selbox.options[selbox.options.length] = new Option('First select a Region','0'); } <? $car_result = mysql_query(" SELECT * FROM Region") or die(mysql_error()); while(@($c=mysql_fetch_array($car_result))) { ?> if (chosen == "<?=$c['id'];?>") { <? $c_id = $c['id']; $mod_result = mysql_query(" SELECT * FROM city WHERE city='$c_id'") or die(mysql_error()); while(@($m=mysql_fetch_array($mod_result))) { ?> selbox.options[selbox.options.length] = new Option('<?=$m['city'];?>','<?=$m['id'];?>'); <? } ?> } <? } ?> } </script> <form name="myform"><div align="left"> <select name="selectcar" size="1" onchange="setOptions(document.myform.selectcar.options [document.myform.selectcar.selectedIndex].value);"> <option value="0" selected>Select a city</option> <? $result = mysql_query(" SELECT * FROM Region") or die(mysql_error()); while(@($r=mysql_fetch_array($result))) { ?> <option value="<?=$r['id'];?>"><?=$r['Region'];?></option> <? } ?> </select><br><br> <select name="selectmodel" size="1"> <option value=" " selected>First select a Region</option> </select><br><br> <input type="button" name="go" value="Value Selected" onclick="alert(document.myform.selectmodel.options [document.myform.selectmodel.selectedIndex].value);"> </div></form> </select> <h4>Choose Sub Category</h4> <select name="subcat"> <?php ?> my end result should be: select Region > then select city > then select something.. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/275085-dynamic-dropdown/ Share on other sites More sharing options...
Christian F. Posted March 1, 2013 Share Posted March 1, 2013 (edited) Please use the [ code][/ code] tags around your code, as it helps make both your post and your code a lot easier to read. Thank you. Edited March 1, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/275085-dynamic-dropdown/#findComment-1415806 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.