【问题标题】:Getting callback results to send to AJAX from php and update table获取回调结果以从 php 发送到 AJAX 并更新表
【发布时间】:2023-03-09 04:57:01
【问题描述】:

我现在正在通过 AJAX 和 PHP 更新数据库表,但我希望我的页面响应更快,并在将数据插入数据库表后更新我的文件中的表。

我不确定如何将信息从我的 php 文件发送到 AJAX,然后如何让 AJAX 更新表格。如何从我的 PHP 文件中回调数据以使我的表格具有交互性?

桌子

    Current Announcements
        <table>
            <tr>
                <th>ID</th>
                <th>Username</th>
                <th>Message</th>
                <th>Date</th>
            </tr>   
<?php
        while ($row = $announcements_stmt->fetch()) {
?>
                <tr>
                    <td><?php echo $announcements_id; ?></td>
                    <td><?php echo $announcements_username; ?></td>
                    <td><?php echo $announcements_messages; ?></td>
                    <td><?php echo $announcements_date; ?></td>
                </tr>   

<?php
        } 
?>

    }
            </table>
<?php           
    }
}

AJAX

$(document).ready(function(){ 
         $("#submit_announcement").on("click", function () {

         var user_message = $("#announcement_message").val();
            //$user = this.value;
             $user = $("#approved_id").val();
            $.ajax({ 
                url: "insert_announcements.php", 
                type: "POST",
                data: {
                       "user_id": $user,
                                    //"message": user_message
                                    "user_message": user_message
                        },
                success: function (data) {
                       //  console.log(data); // data object will return the response when status code is 200
                         if (data == "Error!") {
                             alert("Unable to get user info!");
                             alert(data);
                         } else {
                             $(".announcement_success").fadeIn();
                             $(".announcement_success").show();
                             $('.announcement_success').html('Announcement Successfully Added!');
                             $('.announcement_success').delay(5000).fadeOut(400);
                         }
                     },
                     error: function (xhr, textStatus, errorThrown) {
                         alert(textStatus + "|" + errorThrown);
                         //console.log("error"); //otherwise error if status code is other than 200.
                     }
                 });
             });
         });

PHP 文件

$announcement_user_id= $_POST['user_id'];
$announcement_message= $_POST['user_message'];
$test = print_r($_POST, true); 
file_put_contents('test.txt', $test); 
//var_dump($announcement_user_id);

$con = mysqli_connect("localhost", "", "", "");
$stmt2 = $con->prepare("INSERT INTO announcements (user_id, message, date) VALUES (?, ?, NOW())");
    if ( !$stmt2 || $con->error ) {
        // Check Errors for prepare
         die('Announcement INSERT prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt2->bind_param('is', $announcement_user_id, $announcement_message)) {
        // Check errors for binding parameters
        die('Announcement INSERT bind_param() failed: ' . htmlspecialchars($stmt2->error));
    }
    if(!$stmt2->execute()) {
        die('Announcement INSERT execute() failed: ' . htmlspecialchars($stmt2->error));
    }
        //echo "Announcement was added successfully!";
    else
    {
         echo "Announcement Failed!";
    }

【问题讨论】:

    标签: javascript php jquery ajax callback


    【解决方案1】:

    我认为要做到这一点,您需要以下步骤:

    • 编写一个新的 php 脚本,从数据库中获取项目(如在“表格”脚本中)并以您想要的表格格式回显这些项目。因此,当您调用此脚本时,它只回显一个包含行的表。
    • 从“表格”脚本中删除动态行部分。
    • 在您的 ajax 请求成功后,向新的 php 脚本发出新请求
    • 使用 jQuery(.html()、insertAfter()、appendTo() 或类似的东西)将该脚本的输出放在 html 中。

    `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多