循环提交按钮并发布到MySQL w / ajax

时间:2016-09-27 05:27:06

标签: php jquery mysql ajax

我目前有一个基于数据库中行数创建的表单。每行都被拉入其自己的数据可编辑的形式。每行还有它自己的按钮。

$i = 0; 
  while($row = mysql_fetch_array( $result )) { 
    if ($row['stage']=='1'){ $i++; 
<form method="post" name="form<?php echo $i;?>' id='form<?php echo $i;?>">
<input type="text" name="product<?php echo $i;?>" value="<?php echo $row['product'];?>" id="product<?php echo $i;?>" />
<input type='button' name='submit<?php echo $i;?>' value='NEXT'/> 
</form>

JQUERY

$(".submit1").click(function(){
        $.ajax({
            type: "post",
            url: "xx1.php",
            data: $("#form1").serialize(),
            success: function() {
                alert("UPDATE SUCCESS");
                location.reload();  
            },
            error: function () {
                alert("FAILED");
            }
        });
    });

xx1.php

$ProductOne= strip_tags($_POST['product1']);
 if ($StageOne == "1"){
        SQL STATEMENT
   }elseif ($StageOne == "2"){
         SQL STATEMENT
     }elseif ($StageOne == "3"){

我目前每行都有xx.php(xx1.php,xx2.php,xx3.php),在我的php文件中我的$ _POST [&#39; x&#39;]匹配xx.php($ _POST [&#39; product1&#39;],$ _POST [&#39; product2&#39;],$ _ POST [&#39; product3&#39;]

如何写点击是否点击submitx的内容更新i = i的行(点击submit1时更新product1,点击submit2时更新product2)

1 个答案:

答案 0 :(得分:1)

您需要获取按钮ID(被点击的按钮)并调用相应的文件以执行。

$("button").click(function() {
  //extract the name of the button clicked
  var name = $(this).attr('name');
  //get the button id from name.
  var buttonId = name.split('submit')[1];
  $.ajax({
      type: "post",
      url: "xx.php",
      data: $("#form" + buttonId + "").serialize(),
      success: function() {
          alert("UPDATE SUCCESS");
          location.reload();  
      },
      error: function () {
         alert("FAILED");
      }
  });
});

发送到xx.php的数据具有id。所以你可以使用这个id来操作xx.php。

$id = //get the id from form data sent from js
$Product = strip_tags($_POST["product{$id}"]);
.
.
//use id instead of number in further operations.