基本上我正在制作一个CMS,并希望进行后期编辑。
它的工作原理是博客文章被回显出来(PHP),并且有一个隐藏的ckeditor,当单击编辑按钮时会显示。然后,编辑按钮将替换为保存按钮。
这一切都很好用,但是保存博客文章时会出现问题。
PHP 工作正常,Ajax 验证也工作正常,但仅当有 1 篇博客文章时。
当有超过 1 个帖子时,错误就会出现。问题是保存帖子按钮似乎正在发送每篇博客文章的所有数据。我用萤火虫网检查了一下,发现所有数据都在发送。
我只需要一种方法,以便表单中的保存按钮仅影响该表单中的数据。目前,他们所有人都显示错误/成功消息。
以下是回应的帖子:
<div class="blogtest">
<form action="process/updatepost.php" class="updatepost" method="post">
<input type="button" class='.$editenabled.' value="Edit">
<input type="submit" class="saveupdatebutton" value="Save">
<input type="hidden" class="postid" name="postid" value="'.$postID.'">
<div class="text">
<div class="buildtext">'.$text.'</div>
<div class="editor"><textarea name="ckeditor" class="ckeditor">'.$text.'</textarea></div>
</div>
</form>
</div>
这是javascript:
$(document).ready(function(){ $(".updatepost").submit(function(){
$(".error").remove();
$(".success").remove();
// If there is anything wrong with
// validation we set the check to false
var check = true;
// Get the value of the blog update post
var blogpost = $('.ckeditor').val();
// Validation
if (blogpost == '') {
check = false;
$('.ckeditor').after('<div class="error">Text Is Required</div>');
}
// ... goes after Validation
if (check == true) {
$.ajax({
type: "POST",
url: "process/updatepost.php",
data: $(".updatepost").serialize(),
dataType: "json",
success: function(response){
if (response.databaseSuccess)
$('.ckeditor').after('<div class="success">Post Updated</div>');
else
$('.ckeditor').after('<div class="error">Something went wrong!</div>');
}
});
}
return false;
});
});
感谢您的阅读。希望你能帮到忙。
您只需要将验证逻辑限制为实际提交的表单。现在$('.ckeditor').after('<div class="error">Text Is Required</div>');
正在修改与 ckeditor 类名匹配的所有项目。见下文 - 我添加了一个名为$targetForm的变量,该变量获取正在提交的表单,并适当地修改了代码以仅引用该表单的子项。
$(document).ready( function() {
$(".updatepost").submit(function() {
var $targetForm = $(this);
$targetForm.find(".error").remove();
$targetForm.find(".success").remove();
// If there is anything wrong with
// validation we set the check to false
var check = true;
// Get the value of the blog update post
var $ckEditor = $targetForm.find('.ckeditor'),
blogpost = $ckeditor.val();
// Validation
if (blogpost == '') {
check = false;
$ckeditor.after('<div class="error">Text Is Required</div>');
}
// ... goes after Validation
if (check == true) {
$.ajax({
type: "POST",
url: "process/updatepost.php",
data: $targetForm.serialize(),
dataType: "json",
success: function(response){
if (response.databaseSuccess)
$ckeditor.after('<div class="success">Post Updated</div>');
else
$ckeditor.after('<div class="error">Something went wrong!</div>');
}
});
}
return false;
});
});