gpt4 book ai didi

javascript - 如何防止此ajax请求丢失数据

转载 作者:搜寻专家 更新时间:2023-10-31 21:56:01 25 4
gpt4 key购买 nike

我的博文末尾有一个评论表。到目前为止的测试中,绝大多数评论都成功保存到数据库中,但偶尔页面给人的印象是评论已成功发布 - 但在重新加载页面后,评论消失了(并检查数据库表确认它从来没有做到那么远)。有没有办法在某处修改我的代码以捕获这些异常事件?

我知道 $.ajax 有一个错误函数,但我不认为在这种情况下添加它会有帮助。实际的 ajax 请求似乎在工作——因为它总是运行“成功”函数中的内容。那么也许是 postComment.php 需要修改?

表单提交背后的代码:

if( $(".blogPost").length ) {
$(".commentForm").submit(function(e) {
e.preventDefault();
}).validate({
submitHandler: function (form) {
var url = window.location.pathname;
var post_url = url.substring(url.lastIndexOf('/') + 1);
$("input[name=post_url]").val(post_url);
var formData = $(form).serialize();
var post_id = $(".post").attr("id");
$.ajax({
url:"/postComment.php?PostID=" + post_id,
type:"POST",
data: formData,
success:function(data){
$(".comments").prepend(data);
$("#commentName").val("");
$("#commentEmail").val("");
$("#commentWebsite").val("");
$("#comment").val("");
$(".commentForm input[type='submit']").val('Success!').delay(5000).queue(function(){
$(".commentForm input[type='submit']").val('Post Comment');
});
}
});
}
});

}

postComment.php 页面代码:

<?php
include('dbconnect.php');

$name = $_POST['commentName'];
$email = $_POST['commentEmail'];

$website = $_POST['commentWebsite'];
if( $website != ''){
if ( $ret = parse_url($website) ) {

if ( !isset($ret["scheme"]) )
{
$website = "http://{$website}";
}
}
}

$comment = $_POST['comment'];
$date = date('Y-m-d H:i:s');
$post_id = $_GET['PostID'];

$blogAuthor = '';
if( $name == "Luke Twomey"){
$blogAuthor = "<span> - Blog Author</span>";
}else{
$blogAuthor = false;
}

$SQL = "INSERT INTO comments (name, email, website, comment, date, post_id) VALUES ('$name', '$email', '$website', '$comment', '$date', '$post_id')";
mysqli_query($link, $SQL);

echo "<section class='comment'>
<h3 class='commentAuthor'>$name$blogAuthor</h3>
<a href='$website'><p class='commentAuthorWebsite'>$website</p></a>
<p class='postDate'>$date</p>
<p>$comment</p>
</section>";

$subject = $name . $_POST['subject'];
$post_url = $_POST['post_url'];
$postedMessage = $_POST['comment'];
$contentForEmail = $postedMessage.'<br><a href="http://www.fakedomainhere.com/blog/'.$post_url.'#comments"><p>View comment on website</p></a>';

$header = "From: fake-email-here\n"
. "Reply-To: fake-email-here\n" . "Content-Type: text/html; charset=ISO-8859-1\r\n";

$email_to = "fake-email-here";

mail($email_to, $subject , $contentForEmail, $header );


?>

最佳答案

首先确保插入成功,然后只返回成功信息。

if(mysqli_query($link, $SQL)){
echo "<section class='comment'>
<h3 class='commentAuthor'>$name$blogAuthor</h3>
<a href='$website'><p class='commentAuthorWebsite'>$website</p></a>
<p class='postDate'>$date</p>
<p>$comment</p>
</section>";
}else{
echo "problem while inserting"; //or return an array with some status to tell the user to submit again.
// or header('HTTP/1.0 500 Internal Server Error'); exit;
}

关于javascript - 如何防止此ajax请求丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33668187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com