2016-09-27 45 views
1

我目前有一个根据数据库中的行数创建的表单。每行都被拉到自己的数据可编辑的形式。每一行也有它自己的按钮。通过提交按钮循环并发布到MySQL w/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文件my $ _POST ['x']匹配xx.php($ _POST ['product1'],$ _POST ['product2'],$ _POST ['product3']

如何写一些说if苏bmitx被点击更新该行,其中i = i(点击submit1时更新product1,点击submit2时更新product2)

回答

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. 
+0

感谢您的及时响应。这对按钮有意义。如果我不想手动创建每个xx.php(xx1,xx2)并且我只是想让一个脚本更新单击该按钮的行,这将如何工作? – Billy

+0

@Billy只调用xx.php并将id发送到文件。检查更新后的答案 – jitendrapurohit

+0

这可行。感谢您的帮助! – Billy