使用ajax从数据库获取数据不起作用

时间:2017-05-24 07:47:29

标签: php jquery mysql ajax

我有这个代码:连接到数据库

<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $conn = new mysqli("localhost", "root", "", "jquery");
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

现在我已经在表格中有数据indatabase,名为city witch它只有id和desc,这就是代码

if (isset($_POST['city'])) {
        $sql = "SELECT * FROM city";

        $result = $conn->query($sql);
        if ($result->num_rows > 0) {
            $results = [];
            while($row = $result->fetch_assoc()) {
                $results[] = $row;
            }
            echo json_encode($Results);
        }
        else
        {
            echo "empty";
        }
    }

这是html部分:

 <select required="required" id="city">
                            <option disabled selected value=''> select a city </option>
                        </select>

这是函数:

 function city() {
            $.ajax({
               "url": "divs.php",
               "dataType": "json",
               "method": "post",
               //ifModified: true,
               "data": {
                   "F": ""
               }
            })
            .done(function(data, status) {
                if (status === "success") {
                    for (var i = 0; i < data.length; i++) {
                        var c = data[i]["city"];
                        $('select').append('<option value="'+c+'">'+c+'</option>');
                    }

                }
            })
            .always(function() {

            });
        }

所以问题是选择列表中什么都没有,它总是空的,有什么帮助吗?谢谢你

3 个答案:

答案 0 :(得分:0)

您使用了$results,但在回显时您正在使用$Results这是一个不同的变量,所以请使用,

echo json_encode($results);

另外,您告诉我city只有iddesc因此在JS代码中使用desc代替city

$.ajax({
    type:'POST',
    url: 'divs.php',
    data: {city:1},
    success:function(data) {
        var res = JSON.parse(data);
        $(res).each(function(){
            var c = this.city_name; // use city_name here instead of city
            $('select').append('<option value="'+c+'">'+c+'</option>');
        });
    }
});

答案 1 :(得分:0)

这里有一个小错误: 您正在使用

echo json_encode($Results);

而不是

echo json_encode($results);

在PHP中,变量名称区分大小写。因此,对所有变量使用适当的大小写。

答案 2 :(得分:0)

您无法获取HTML <SELECT> TAG中的响应数据。这是您可以执行的操作。

图像到表格 city table image

HTML文件代码:

 <!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <form action="" method="post">
        <select>

        </select>
    </form> 
<script>
$(document).ready(function(){
    city();
});

function city(){
     $.ajax({
            type:'POST',
           url: 'divs.php',
           data: {city:1},
           success:function(data){
                var res = JSON.parse(data)

                for(var i in res){
                    var showdata = '<option value="'+res[i].city_name+'">'+res[i].city_name+'</option>';

                    $("select").append(showdata);
                }
           }
        });
}
</script>
</body>
</html>

这是PHP代码

`

<?php
    $conn = new mysqli('localhost','root','','demo');

    if($conn->connect_error){
        die ("Connection Failed".$conn->link->error);
    }

    if(isset($_POST['city'])){
        $sql = "SELECT * FROM city";

        $result = $conn->query($sql);

        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {
                $results[] = $row;
            }
            echo json_encode($results);
        }
        else
        {
            echo "no city available";
        }
    }
?>

`

这是输出图像

OUTPUT RESULT

相关问题