【问题标题】:Ajax call to php, get mysql data as array and use in JS functionAjax 调用 php,获取 mysql 数据作为数组并在 JS 函数中使用
【发布时间】:2018-02-27 15:56:22
【问题描述】:

我希望对 PHP 脚本进行 ajax 调用以从 MySQL 获取数据,创建一个 json 数组并将其传递回 ajax 调用的成功函数,然后我将在其中将其用作 JavaScript 的参数功能。

这是我的ajax调用,

    $('button[name="message"]').click(function() {
    var $row = $(this).closest("tr");    // Find the row
    var $tenant_id = $row.find(".col-md-1 id").text(); // Find the tenants ID
    var $landlord_id = "<?php echo $id; ?>"
    $.ajax({
        url : "./message.php",
        type : "POST",
        async : false,
        data: {
        landlord_id: $landlord_id,
        tenant_id : $tenant_id
        },
        success: function(data){
            console.log(data);
            var messages = data;
            insertChat(messages.sender_id, messages.body, messages.timestamp);
        }
        })
});

这是我的 PHP 文件,

    <?php
    session_start();
    require_once('../dbconnect.php');
    // update tenants table to show deposit returned
    if(isset($_POST['tenant_id'])){
    $tenant_id = $_POST['tenant_id'];
    $landlord_id = $_POST['landlord_id'];

    $sql = "SELECT * from messages  WHERE messages.sender_id OR messages.receiver_id = '$tenant_id' AND messages.sender_id OR messages.receiver_id = '$landlord_id'";
    $result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));

    //create an array
    $messages = array();
    while($row =mysqli_fetch_assoc($result))
    {
        $messages[] = $row;
    }
    echo json_encode($messages);

}
    ?>

如果有人有教程或各个部分的链接,那就太好了。我什至不知道我上面概述的过程是否正确。 如果有人能告诉我正确的方法来解决这个问题,那将有很大帮助!

谢谢

【问题讨论】:

  • 你的逻辑好像不错,别忘了你成功的JSON.parse(data)
  • 那么您的问题到底是什么?运行上述内容时会发生什么?仅供参考,这里要求提供教程链接显然是题外话。
  • 网上有很多教程。您可以使用 google 轻松找到它们。本网站是为了在您卡在教程上时为您提供帮助,而不是请求资源。
  • 好吧,一种“正确”的方法是对 SQL 部分使用准备好的语句,这样您就不会受到 sql 注入攻击。其余的取决于您喜欢如何处理 ajax 调用。
  • @IncredibleHat 效果很好,非常感谢。我之前尝试过一个循环,但我正在做 data.sender_id[i] 而不是数据上的循环。非常感谢!我现在将开始使用准备好的语句,再次感谢!

标签: javascript php arrays json ajax


【解决方案1】:

调整你的javascript方面的一些事情(我不会解释你遇到的php sql注入问题......但请研究preparebind_paramexecute):

  1. 由于您要从 php (json_encoded) 返回一个 $messages 数组,因此您需要在 success 处理程序中循环这些数组。

  2. dataType: 'JSON' 添加到您的选项中,因此它明确期望从 php 返回的 json。

  3. 你还少了几个分号;)

添加到您的代码的调整:

$('button[name="message"]').click(function() {
    var $row        = $(this).closest("tr");
    var tenant_id   = $row.find(".col-md-1 id").text();
    var landlord_id = "<?php echo $id; ?>";
    $.ajax({
        url : "./message.php",
        type : "POST",
        data: {
            landlord_id: landlord_id,
            tenant_id : tenant_id
        },
        dataType: 'JSON',
        success: function(data){
            console.log(data);
            if (typeof data !== undefined) {
                for(var i = 0; i < data.length; i++) {
                    insertChat(data[i].sender_id, data[i].body, data[i].timestamp);
                }
            }
        }
    });
});

【讨论】:

    猜你喜欢
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2013-12-12
    • 2023-01-16
    • 2011-03-31
    • 1970-01-01
    • 2013-01-11
    相关资源
    最近更新 更多