【发布时间】:2016-05-19 23:16:22
【问题描述】:
我有一个 HTML 表单,它会在插入查询成功后返回为带有 JQuery Ajax 的 HTML 格式。
comment.php
$post_id=$_POST['id'];
mysql_query("INSERT INTO comment(comment,post_id,)VALUES('$comment','$post_id')");
if(!mysql_errno()){
?>
<p><?php echo $comment; ?></p>
这是我的 JQuery 代码,它将在插入查询成功后发送请求和值并返回 HTML 表单。
index.php
// on post comment click
$('.bt-add-com').click(function(){
var theCom=$(this).siblings('.the-new-com');
if(!theCom.val()){
alert('You need to write a comment!');
}else{
var post_id=$(this).parents(".post_id").attr("id");
$.ajax({
type: "POST",
url: "comment.php",
data: "act=add-com&comment="+theCom.val()+"&id="+post_id,
success: function(html){
theCom.val('');
$('.the-new-com').hide('fast', function(){
$('.new-comment-line').show('fast');
$('.new-comment-line').after(html);
});
}
});
}
});
这是我的表格。它每次都在用户提交的帖子循环中运行。
<form action="" method="POST" class="post_id" id="<?php echo $post_id; ?>">
<span>Write a comment ...</span>
</div>
<div class="new-comment-line"></div><----here is a line before the comment initiates..--->
<textarea class="the-new-com"></textarea>
<div class="bt-add-com">Post comment</div>
</form>
现在我的问题是我的所有代码都运行良好,除了最后几行 JQuery 代码。
$('.new-comment-line').after(html);
Ajax请求后我的表单从comment.php返回HTML表单时的代码行有问题。表单会在每次用户提交状态时在每个帖子中打印。
我应该如何处理表格?我希望我的 cmets 每次只在用户提交的特定帖子中打印。
【问题讨论】:
-
请使用 Mysqli 或 PDO,因为 mysql 在较新的 PHP 版本中已被弃用,并在查询中转义您的变量。您的代码根本不是注入证明。有关如何执行此操作的更多信息:php.net/manual/en/mysqli.real-escape-string.php
-
兄弟!我很清楚mysql现在是折旧版本。我的所有任务不仅仅是关于 php,它与 jquery 相关,这只是一个代码示例,以便更容易理解,而且我也在使用 mysqli 程序。所以请给我解决方案如何处理循环通过带有jquery的表单,我只有在循环中创建的许多元素的同一类。那么我如何处理每个表单上特定点击事件的特定元素。就是这样。
标签: javascript php jquery html ajax