繁体   English   中英

如何显示评论表单提交而不使用AJAX刷新页面?

[英]How to display comment form submit without refresh the page using AJAX?

评论表单正在提交,数据也已保存到数据库。 但是如果不刷新页面就不会在浏览器中显示。

这是代码:

$("#post_reply").click(function (event) {
    $("#data_status").html("");
    $('#ajax_loading').show();
    event.preventDefault();
    if (document.getElementById('_comment').value.trim() == "") {
        return false;
    }
    $.post('../services/leave_comment.php', $("#open_status").serialize(), function (data) {
        $('#ajax_loading').hide();
        if (data.split("::")[1] == true) {
            $("#data_status").html("Commented Successfully..");
            $("#data_status").fadeOut(3000);
            document.getElementById('_comment').value = '';
            $('#_comment').html("");

        } else if (data.split("::")[1] == false) {
            $("#data_status").html("Error occured in Comment Submission.. TryAgain..");
            $("#data_status").fadeOut(3000);
        }
    });
});

编辑:

我所能理解的是我还没有使用ajax发布数据吗? 这是我需要做的吗?

$("#post_reply").click(function (event) {
$("#data_status").html("");
$('#ajax_loading').show();
event.preventDefault();
if (document.getElementById('_comment').value.trim() == "") {
    return false;
}
$.post('../services/leave_comment.php', $("#open_status").serialize(), function (data) {
    $('#ajax_loading').hide();
    if (data.split("::")[1] == true) {
        $("#data_status").html("Commented Successfully..");
        $("#data_status").fadeOut(3000);
        document.getElementById('_comment').value = '';
        $('#_comment').html("");


$.ajax({
type: 'POST',
url : 'http://localhost/tech1/services/get_more_comments.php',  
data: 'last_id='+last_id,
success: function(data){
$('.view_container').append(data);
},
complete: function(){
console.log('DONE');
                    }                       
                });

    } else if (data.split("::")[1] == false) {
        $("#data_status").html("Error occured in Comment Submission.. TryAgain..");
        $("#data_status").fadeOut(3000);
    }
});
 });

您所做的所有代码就是将数据发布到服务器。 没有什么可以从服务器获取新评论或手动附加已发布的评论。 您可以再次使用ajax刷新评论,或者更简单地在发布内容后添加评论。

我会说要在网上搜索jQuery的.load

例:

function updateShouts(){
   // Assuming we have #shoutbox
   $('#shoutbox').load('latestShouts.php');
}

在这种情况下, shoutbox将是包含您的注释的div,您将在ajax发布成功时调用此函数,latestshouts.php将仅包含该div的内容。

有点难以解释,我希望这对你有意义

链接: http//api.jquery.com/load/

暂无
暂无

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

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