获取回调结果从php和更新表发送到AJAX

时间:2015-08-14 17:57:36

标签: javascript php jquery ajax callback

我现在正在通过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!";
    }

1 个答案:

答案 0 :(得分:0)

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

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

`