Jump to content

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/275085-dynamic-dropdown/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.