gpt4 book ai didi

php - 循环提交按钮并使用 ajax 发布到 MySQL

转载 作者:行者123 更新时间:2023-11-28 23:25:05 24 4
gpt4 key购买 nike

我目前有一个根据数据库中的行数创建的表单。每行都被拉入其自己的表格,其中数据是可编辑的。每行也有自己的按钮。

$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['x'] 匹配 xx.php ($_POST['product1' ], $_POST['product2'], $_POST['product3']

如果单击 submitx,如何写一些内容来更新 i = i 的行(单击 submit1 时更新 product1,单击 submit2 时更新 product2)

最佳答案

您需要获取按钮 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.

关于php - 循环提交按钮并使用 ajax 发布到 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39716609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com