将mySQL结果加载到选择列表中

时间:2019-05-27 10:49:18

标签: php mysql

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

<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。

1 个答案:

答案 0 :(得分:-2)

尝试一下

<?php 
include "config.php";
if ($_POST) 
{
$list = $_POST['list'];
if ($list != '') {
    $sql = "SELECT customer_name FROM customers WHERE age=" . $list;
    $result = $link->query($sql);
    echo "<select class='form-control' name='namelist'>";
    echo "<option value=''>Select</option>";
        while($row = $result->fetch_assoc())  
        {
            echo "<option value='".$row['customer_name ']."'>".$row['customer_name ']."</option>";
        }
        echo "</select>";
    }
}
?>