[英]forms of same id in while loop - how to post formdata of user submitted form to php with jQuery AJAX?
我有一個 while 循環,它創建如下形式:
<?php
$i = 1;
while($i<10){
?>
<form id="update">
<tr>
<th scope="row"><?php echo $i;?></th>
<td></td>
<td></td>
<td></td>
<td><input type="text" maxlength="5" class="input-xs valid" name="plus" value="" /></td>
<td><input type="submit" name="submit" class="btn btn-info btn-sm" value="Update" /></td>
<td><input class="input-sm slip" name="slip" value="" disabled /></td>
<td></td>
</tr>
</form>
<?php $i++; } ?>
我想用 jQuery AJAX 發布用戶提交表單的表單數據。
您的問題不清楚,但這是答案 - 如果您基於 while 循環生成表單,那么您需要在表單的每個位置傳遞$i
<?php
$i = 1;
while($i<10){
?>
<form id="update_<?php echo $i ?>">
<tr>
<th scope="row"><?php echo $i;?></th>
<td></td>
<td></td>
<td></td>
<td><input type="text" maxlength="5" class ="input-xs valid" id="plus_<?php echo $i?>" name="plus" value=""></td>
<td><input type="button" name="submit" class="btn btn-info btn-sm" value="Update" onclick="sub_form(<?php echo $i; ?>)"></td>
<td><input class="input-sm slip" id="slip_<?php echo $i?>" name="slip" value="" disabled/></td>
<td></td>
</tr>
</form>
<?php $i++; } ?>
AJAX
function sub_form(val){
var plus = document.getElementById('plus_'+val).value;
var slip = document.getElementById('slip_'+val).value;
$.ajax(
{
type:"post",
url: "your url here",
data:{ plus:plus, slip:slip},
success:function(response)
{
}
}
);
}
希望它對你有用
您應該在表單 HTML 節點中添加一個類,如下所示:
<form class="anyclass" action="add your action where you want to post form">
在此之后,您可以提交如下表格:
$(".anyclass").submit(function(e) {
e.preventDefault
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data)
{
alert('form submitted herer');
}
});
希望對你有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.