Javascript 如何使用AJAX显示评论表单提交而不刷新页面?
评论表单正在提交,数据也将保存到数据库中。但在不刷新页面的情况下不显示在浏览器上 代码如下:Javascript 如何使用AJAX显示评论表单提交而不刷新页面?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,评论表单正在提交,数据也将保存到数据库中。但在不刷新页面的情况下不显示在浏览器上 代码如下: $("#post_reply").click(function (event) { $("#data_status").html(""); $('#ajax_loading').show(); event.preventDefault(); if (document.getElementById('_comment').value.trim() == "") {
$("#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刷新注释,或者更简单地使用发布的内容附加注释。我想在web上搜索jQuery的
。load
:
例如:
function updateShouts(){
// Assuming we have #shoutbox
$('#shoutbox').load('latestShouts.php');
}
在本例中,shoutbox
将是包含您的注释的div,
您可以在ajax帖子成功后调用此函数
latestshouts.php将只包含该div的内容
有点难以解释,我希望这对你有意义
链接:什么不显示?@GertB。不显示注释。我碰巧刷新了页面来查看评论。我没有看到在你的代码中你在页面上添加评论的地方。你的php代码在刷新页面之前不会更改,你可以使用jquery或Javascript添加评论。我不明白你说的是什么,我是这个脚本的新手,请详细说明。