更改第一个</select>后更改第二个<select>标记上的值

时间:2016-07-22 16:54:30

标签: php jquery

标题说明了我的目标。我试图在第一个标签更改时更改第二个选择标记的值。这是我尝试过的。值来自数据库。所以这将涉及一些PHP选择。

<div class="row">
      <div class="col-md-5">
        <label for="project">From Project</label>
        <select class="form-control"id="project"onchange="dropDrown(this.value)" name="project">
        <?php
        $sql = $db->prepare("SELECT * FROM tbl_project WHERE projectStatus = 1");
        $sql->execute();
        while($result=$sql->fetch(PDO::FETCH_ASSOC)){
          $value        = $result['projectID'];
          $projectName  = $result['projectName'];
        echo"
        <option value='$value'> $projectName  </option>
        ";
        }
        ?>
        </select>
      </div>
    </div>
    &nbsp;
            <div class="row">
                <div class="col-md-5">
                    <select class="form-control" id="village" name="village"></select>
                </div>
            </div>

AJAX:

 <script type="text/javascript">
function dropDown(id){
    var theID = id;
// assign your data to a varaible
var dataString= {theID:id};
$.ajax({
        url:    "includes/getVillage.php",
        type:   "POST",
        data:   dataString,
        cache:  false,
        success: function (data){
            $("#village").html(data);
        }
});

}

getVillage.php

<?php
include '../../connection';
$village = $_POST['theID'];
$sql                   =    "SELECT * FROM  tbl_village WHERE projectID = '$village'";
$query               =  $db->prepare($sql);
$results            =   $query->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
  $value = $results['villageID'];
  $text  = $results['villageName'];

  echo "<option value'$value'>$text</option>";
}

2 个答案:

答案 0 :(得分:1)

您似乎没有通过ajax调用传递数据。

function dropDown(id){
    var theID = id;
    // assign your data to a varaible
    var dataString= {theID:id};
    $.ajax({
            url:    "includes/getVillage.php",
            type:   "POST",
            data:   dataString,
            cache:  false,
            success: function (data){
                $("#village").html(data);
            }
    });
}

或者,您可以传递以下格式的值

var  dataString= "theID="+id;

答案 1 :(得分:0)

事实证明我做错了连接。仅评估getVillage.php文件后。我收到了很多关于connection.php的错误。谢谢你们。