php - 将表单数据从ajax加载的记录发送到php

标签 php jquery mysql ajax

我正在尝试使用ajax发送表单数据。我的索引页记录在使用 ajax 从远程页面获取后显示。

下面是我的页面图片。

indexpage image

每条记录都有评论框我想使用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/

相关文章:

jquery - 禁用第一个子 div 中的按钮

mysql - 一起使用 MySQL 和 RedShift 的坏习惯?

php - 使用 Ajax 验证动态下拉列表

php - MySQL SELECT 条件基于最后一行和日期间隔

javascript - 用javascript替换html部分

javascript - AngularJS 也获取空数据

javascript - 获取所有选定的值()'s from a multiselect container per it'自己点击:event?

mysql - 为 mySQL 重置 root pw

php - 如何将原始查询替换为 laravel 格式

php - 如何在 Laravel 中启用 CORS?