Jump to content

Dropdown auto Populate with Multiple Select


brooksh

Recommended Posts

I already have a working script that will let you select a state from the drop down and in another drop down display all the cities. I am trying to a select multiple states, then display all cities in those multiple states.

 

data.php

if ($_GET['act']=='getcity') { 
$query_city = sprintf("SELECT city,ID FROM database WHERE state = '%s' ORDER BY city ASC", $_GET['state']);
$city = mysql_query($query_city);
$row_city = mysql_fetch_assoc($city);
//$totalRows_groups = mysql_num_rows($groups);
do {
	$options[] = "{optionValue:'".$row_city['city']."', optionDisplay: '".htmlentities(trim($row_city['city'])) ."'}";
} while ($row_city = mysql_fetch_assoc($city));
//	print_r($options);
$theOptions = implode(",", $options);
$theOptions = "[".$theOptions."]";

echo $theOptions;

}

 

 

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" charset="utf-8">

$(function(){
  $("select#state").change(function(){
    $.getJSON("data.php",{act: 'getcity', state: $(this).val()}, function(j){
      var options = '';
        options += '<option value="">All</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';

      } 
      $("select#city").html(options);
    })
  })
})
</script>


<?
$query_state = "SELECT DISTINCT state FROM database ORDER BY state ASC";
$state = mysql_query($query_state);
$row_state = mysql_fetch_assoc($state);
?></font><select name="state_filter[]"  id="state" onchange="ShowTB(this,'state3');enableSelect('cityselect');"  style="font-size: 10pt; font-face:Arial" size="4" multiple>

 <option value="">Select One</option>
<?php do {  ?>
		 <option value="<?= $row_state['state']; ?>" <? if($state_filter == $row_state['state']){echo "selected";} ;?>><?= $row_state['state']; ?></option>
		 <?php } while ($row_state = mysql_fetch_assoc($state)); ?></select></font></td>
	</tr>
	<tr>
		<td align="right"><font face="Arial" size="2">city: </font> </td>
		<td><font face="Arial"><select name="city_filter[]"  id="city" id2="cityselect"  onchange="ShowTB(this,'city3');"   style="font-size: 10pt; font-face:Arial" size="4" multiple><? 
if($state_filter == ""){
echo '<option value="">...................</option>';
}
if($state_filter != ""){
$query=mysql_query("SELECT DISTINCT city FROM database WHERE state='$state_filter' order by city ASC");
while($noticia2 = mysql_fetch_array($query)) { 
echo "<option value='$noticia2[city]'";
if($noticia2['city']==$city_filter){echo "selected";}
echo ">$noticia2[city]</option>";
}}
?>
       </select>

Link to comment
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.