我正在尝试使用ajax发送表单数据。我的索引页记录在使用 ajax 从远程页面获取后显示。
下面是我的页面图片。
每条记录都有评论框我想使用ajax将这些评论存储在数据库中。
下面是我的jquery
$(function(){
$('body').on('submit','.comment_p',function(){
var post_id = $("#post_id").val();
var com_dis= $("#comment_disc").val();
var cominfo = 'id=' + post_id + '&disc=' + com_dis;
if(com_dis=='')
{
alert('Please add your comment');
} else{
$.ajax({
type:"POST",
url:"/comment_update.php",
data:$(".comment_p").serialize(),
success: function(data){
alert(data);
}
});
}
return false;
});
});
我在点击时使用了body
,因为这些记录是从远程页面加载的。
我的表格如下
<div class="panel-zesteve-textarea">
<form method="post" id="'.$row['id'].'" class="comment_p">
<input id="post_id" type="hidden" name="post_id" value="'.$row['id'].'">
<textarea id="comment_disc" name="comment_disc" rows="2" cols="48"></textarea>
<button id="com_submit" type="submit" class="btn btnbg">Post comment</button>
<button type="reset" class="[ btn btn-default ]">Cancel</button>
</form>
现在,当我单击帖子时,评论适用于一条记录,即最后一条记录。我需要发送身份证号码。和 textarea 值到 php 页面以在 mysql 中更新,但两个注释都显示相同的记录 id 和注释。第二个不起作用
最佳答案
尝试使用$(this)
引用表单
data: $(this).serialize(), instead of `data:$(".comment_p").serialize(),`
关于php - 将表单数据从ajax加载的记录发送到php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36526308/