【问题标题】:get data from database using ajax not working使用ajax从数据库中获取数据不起作用
【发布时间】:2017-05-24 07:47:28
【问题描述】:

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

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

现在我已经在名为 city 的表中的数据库中有数据,它只有 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() {

            });
        }

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

【问题讨论】:

  • $results$Results 不同
  • 也许在你的 PHP 部分中,我们有变量 $results 你有一个大写的 $Results 可能是问题?

标签: php jquery mysql ajax


【解决方案1】:

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

echo json_encode($Results);

而不是

echo json_encode($results);

在 PHP 中,变量名区分大小写。因此,请为所有变量使用正确的大小写。

【讨论】:

    【解决方案2】:

    您没有在 HTML &lt;SELECT&gt; TAG 中获取响应数据,这是您可以做的。

    图像到表格

    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";
            }
        }
    ?>
    

    `

    这是输出图像

    【讨论】:

      【解决方案3】:

      您使用了$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>');
              });
          }
      });
      

      【讨论】:

      • 如果这是唯一的问题,那么为什么不将其标记为由于拼写错误而关闭?
      • @adamspaul 如果你知道问题,你为什么要问这个问题? O.o
      • 我不知道问题出在哪里@Andreas 我要求弄清楚在哪里
      • @adamspaul 立即尝试。
      猜你喜欢
      • 1970-01-01
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-12-15
      • 2019-11-26
      • 2023-03-27
      • 1970-01-01
      • 2014-06-09
      相关资源
      最近更新 更多