简体   繁体   English

使用JQuery / Ajax从Php while循环提交表单

[英]Submit a form from a Php while loop using JQuery/Ajax

I have searched and searched and havent been able to fix my issue.. help ;) 我已经搜索了并且没有能够解决我的问题..帮助;)

I have a form I create in a while loop. 我有一个在while循环中创建的表单。 I then use AJAX to process it.. Problem is it only will sumbit first form, even if I click the second form. 然后,我使用AJAX进行处理。问题是,即使单击第二个表单,它也只会汇总第一个表单。 Assume this is due to each form needing a unique ID. 假设这是由于每个表单都需要唯一的ID。 Im having a problem doing that... any help would be wonderful. 我在执行此操作时遇到问题...任何帮助都会很棒。

My Php for form 我的PHP表格

echo '<table width="100%" border="0" cellspacing="0" cellpadding="0" >';
echo ' <tr>';
echo ' <td style="color:#bbb">Team Name</td>';
echo '<td style="color:#bbb">Event Name</td>';
echo ' <td style="color:#bbb">Event Level</td>';
echo ' <td style="color:#bbb">Comments</td>';
echo '<td style="color:#bbb">&nbsp;</td>';
echo ' <td style="color:#bbb">&nbsp;</td>';
echo ' </tr>';

while ($row = mysql_fetch_array($pendingresult)) {
    $id = $row['reg_id'];
    print "<form id=\"$id\" name=\"CDs\" method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
    echo '<tr class="commentContainer">';
    echo "<td><input type=\"text\" name=\"team_name\" value=\"$row[team_name]\"</td>";
    echo "<td><input type=\"text\" name=\"reg_id\" value=\"$row[reg_id]\"</td>";
    echo "<td><input type=\"text\" name=\"team_level\" value=\"$row[team_level]\"</td>";
    echo "<td><input type=\"text\" name=\"notes\" value=\"$row[comments]\"</td>";
    echo "<td>";
    echo "<td class=\"delete\" align=\"center\" id=" . $row['reg_id'] . " width=\"10\"><a href=\"#\" id=\"$row[reg_id]\"><img src=\"admin/images/delete.png\" border=\"0\" ></a></td>";
    echo "<td class=\"approve\" align=\"center\" id=" . $id . " width=\"10\"><a href=\"#\" id=\"$row[reg_id]\"><img src=\"admin/images/approve.png\" border=\"0\" ></a></td>";
    echo "</td>";
    echo "</tr>";
    echo "</form>";
}

My AJAX 我的AJAX

$(document).ready(function () {});
$(function () {
  $(".approve").click(function () {
    var commentContainer = $(this).parent('tr:first');
    var id = $(this).attr("id");
    var string = 'id=' + id;
    var formData = this.form.id;
    $.ajax({
      type: "POST",
      url: "approve.php",
      data: $("formData").serialize(),
      cache: false,
      success: function () {
        commentContainer.slideUp('slow', function () {
          $(this).remove();
        });
      }
    });
    return false;
  });
});

In your ajax change 在你的ajax变化

data: $("formData").serialize() 数据:$(“ formData”)。serialize()

to

data: $("form#"+id).serialize() 数据:$(“ form#” + id).serialize()

it will catch the current form you are processing 它将捕获您正在处理的当前表格

You are using form fields in while loop so that means they are array of input fields with same name. 您在while循环中使用表单字段,这意味着它们是具有相同名称的输入字段的数组。

So you need to combine them into one variable and pass it accordingly. 因此,您需要将它们组合为一个变量,并相应地传递它。

Example

var team_name = $("input[name=team_name]").map(function(){
   return $(this).val();
}).get().join(",");

And So on for others fields. 对于其他领域,依此类推。

Try it. 试试吧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM