简体   繁体   English

使用jquery,php和mysql进行级联下拉

[英]making cascading dropdown using jquery,php and mysql

am trying to make a cascading dropdown, i want when a user select region then city dropdown is populated accordingly.. 我正在尝试进行级联下拉菜单,我想在用户选择区域时相应地填充城市下拉菜单。

JS : JS

$(document).ready(function() {
    $('#region').change(function() {
        var region = $(this).val();
        $.post('get_region.php', {
            region: region
        }, function(data) {
            $('#district_div').html(data);
        });
    });

PHP : PHP的

<?php
require_once('../db/connect.php');

$region=$_POST['region'];

$q=mysql_query("select name from city where region='$region'");

$row=mysql_fetch_array($q);
echo $row['name']; 
?>

HTML* strong text * HTML * 强文本 *

 <div class="controls">

                       <select class="bootstrap-select" name="region" id="region">
                                          <option value="">Choose</option>
                                      //from database
                                          <?php echo $region_result; ?>

           </select>
  </div>

                       <select class="bootstrap-select" name="district" id="district" >

                             <div id='district_div'></div>         

          </select>
-for JS it should be like this-

$(document).ready(function() {
    $('#region').change(function() {
        var region = $(this).val();
        $.post('get_region.php', {
            region: region
        }, function(data) {
            $('#district').html(data); // I change this part
        });
    });


-for php-

<?php
require_once('../db/connect.php');

$region=$_POST['region'];

$q=mysql_query("select name from city where region='$region'");

while($row = mysql_fetch_array($q)){
   echo "<option>".$row['name']."</option>";
}

?>


-for the html

<div class="controls">
   <select class="bootstrap-select" name="region" id="region">
      <option value="">Choose</option>
      //from database
      <?php echo $region_result; ?>
    </select>

     <select class="bootstrap-select" name="district" id="district" >

     </select>     <!--you don't need to put a div in the select tag-->
</div>

You are trying to write plain text inside a select box. 您正在尝试在选择框中写入纯文本。 Try to write in HTML format: 尝试以HTML格式编写:

<option>name</option>

In your JS, replace the line: 在您的JS中,替换以下行:

$('#district_div').html(data);

With: 带有:

$('#district').html(data);

Delete the DIV with id "district_div". 删除ID为“ district_div”的DIV。 You cannot have a DIV inside a SELECT. SELECT内不能有DIV。 In PHP, the last line is: 在PHP中,最后一行是:

echo "<option>$row[name]</option>";

you mis some pieces 你错了一些

// will echo the name value of one row
    $row=mysql_fetch_array($q);
    echo $row['name']; 

try 尝试

$results = array();
while($row = mysql_fetch_array($q)){
$results[] = '<option>.'$row['name'].'</option>';
}
$optionString= implode(' ', $results);
echo $optionsString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM