将mySQL结果加载到select

时间:2019-05-27 11:28:24

标签: php mysql ajax

我正在尝试将客户名称加载到选择下拉列表中。第一个下拉列表应导致第二个下拉列表被更新。但是,由于列表仍然为空(仅显示“选择”),因此我难以实现。

<select class="form-control" id="typelist" name="typelist" onchange="get_namelist()";>
<option value="cases">Case</option>
<option value="processors">Processor</option>
</select>

<div id="get_namelist"></div>

<script type="text/javascript">
    function get_namelist() { // Call to ajax function
var typelist = $('#typelist').val();
var dataString = "typelist="+typelist;
$.ajax({
    type: "POST",
    url: "mysubselect.php", 
    data: dataString,
    success: function(html)
    {
        $("#get_namelist").html(html);
    }
});
}   
</script>

mysubselect.php:

<?php
include "config.php";
if ($_POST){
    $list = $_POST['typelist'];
    echo("<script>console.log('PHP: ".$list."');</script>");
    if ($list != '') {
        $sql = "SELECT name FROM parts WHERE type=" . $list;
        $result = $link->query($sql);
        echo "<select class='form-control' name='partlist'>";
        echo "<option value=''>Select</option>";
        while($row = $result->fetch_assoc())
        {
            echo "<option value='".$row['name']."'>".$row['name']."</option>";
        }
        echo "</select>";
    }
    else{
        echo "";
    }
}
?>

第二个列表为空,仅输出Select。我究竟做错了什么?谢谢。

0 个答案:

没有答案