简体   繁体   English

将数据保存到MySQL并使用该数据更新页面而不刷新

[英]Save data to MySQL and update page with that data without refresh

I am trying to save data from a comment form into a MySQL database then update the page with that comment without the user having to refresh. 我正在尝试将评论表单中的数据保存到MySQL数据库中,然后使用该注释更新页面,而无需用户刷新。 Similar to how Facebook comments work. 类似于Facebook评论的工作方式。

I've searched all over the place for an answer to this, but I haven't found one that suites my needs. 我到处搜索了这个问题的答案,但我找不到符合我需求的答案。

Here is the AJAX that submits the form data to the php script: 这是将表单数据提交到php脚本的AJAX:

var ajaxSubmit = function(formEl) {
    // fetch where we want to submit the form to
    var url = $(formEl).attr('action');

    // fetch the data for the form
    var data = $(formEl).serializeArray();

    // setup the ajax request
    $.ajax({
        url: url,
        data: data,
        dataType: 'json',
        success: function(data) {
            if(rsp.success) {
                alert("Comment Successfully Added!");
        }
    });
  return false;
}

I know the page will NOT be updated with this script currently because I am calling an alert. 我知道目前不会使用此脚本更新页面,因为我正在调用警报。 However, when I submit the data I am being taken to the /ajax/comment.process.php page which is the page that calls the insertComment() function and inserts the comment to the database. 但是,当我提交数据时,我将被带到/ajax/comment.process.php页面,该页面是调用insertComment()函数并将注释插入数据库的页面。 I have the alert() function in the success() function right now and I am not even getting that. 我现在在success()函数中有alert()函数,我甚至没有得到它。

What I want is when the comment is submitted, the user doesn't leave the current page, it is just updated with what they just submitted. 我想要的是,当提交评论时,用户不会离开当前页面,只是更新他们刚提交的内容。

Here is the code in /ajax/comment.process.php' 这是/ajax/comment.process.php'的代码

    session_start();

include_once('../classes/comment.class.php');
include_once('../classes/db.class.php');

$user_id = $_SESSION['user_id'];

$db = new DBConnection;
$comments = new Comment($db);

$blog_id = intval($_POST['blogID']);

$addCom = $comments->addComment($user_id);

if($addCom === FALSE) {
    $resp = "<span class='error'>Error adding comment</span>";
} else {

    $resp = $comments->getComments($blog_id);
}

return $resp;

This script calls the insertComment() function which saves the comment to the database, then if that returns true, it calls the getComments() function which retrieves the comments for that particular post and stores them in an array. 此脚本调用insertComment()函数将注释保存到数据库,然后如果返回true,则调用getComments()函数,该函数检索该特定帖子的注释并将其存储在数组中。

The comments ARE successfully being saved to the database, but I am redirected to the ajax/comment.process.php page which is blank. 这些评论被成功保存到数据库中,但我重定向到ajax/comment.process.php页是空白。 How can I update the current page with the comment they posted without having to refresh the page? 如何使用他们发布的评论更新当前页面而无需刷新页面? I thought returning the $resp variable would do it then I could just do a foreach() loop to display them, however that is clearly not the case. 我认为返回$resp变量会执行它然后我可以只做一个foreach()循环来显示它们,但显然不是这种情况。

EDIT: I have implemented EVERYTHING suggested in the answers below and I have still not yet fixed this issue. 编辑:我已经在下面的答案中提出了一切建议,我还没有解决这个问题。 The form is STILL being submitted to /ajax/comment.process.php even when I have these three things that should prevent the form from being submitted: preventDefault(); 表单是STILL提交给/ajax/comment.process.php即使我有三个应该阻止提交表单的东西: preventDefault(); , return false; return false; and return ajaxSubmit(this); return ajaxSubmit(this);

In the ajax you could delete the dataType: 'json', and delete the if(rsp.success) { and make a simple alert 在ajax中你可以删除dataType: 'json',并删除if(rsp.success) {并发出一个简单的警报

$.ajax({
        url: url,
        data: data,
        success: function(data) {
                alert("Comment Successfully Added!");
                alert(data);
        }
});

In the php instead of the return you are using, use echo 在php而不是你正在使用的返回中,使用echo

echo $resp;

At least you will see if there is an error 至少你会看到是否有错误

After that you could start using the json code 之后,您可以开始使用json代码

In the php 在PHP中

echo json_encode($resp);//as soon as $resp is an array

and in the ajax you could alert like this alert(data.keyofthearray) 并且在ajax中你可以像这个alert(data.keyofthearray)一样alert(data.keyofthearray)

To prevent the form from submitting(which is what is happening) use onsubmit="return ajaxSubmit(this);" 要防止表单提交(正在发生的事情),请使用onsubmit="return ajaxSubmit(this);"

also you have a syntax error in your ajax code. 你的ajax代码中也有语法错误。 You never close the if block 你永远不会关闭if

var data = $(formEl).serialize();
$.ajax({
    url: url,
    data: data,
    dataType: 'json',
    success: function(data) {
        if(data.success) {
            alert("Comment Successfully Added!");
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM