[英]forms of same id in while loop - how to post formdata of user submitted form to php with jQuery AJAX?
I have a while loop which creates forms like:我有一个 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++; } ?>
I want to post the formdata of user-submitted form with jQuery AJAX.我想用 jQuery AJAX 发布用户提交表单的表单数据。
your question is not clear but here is the answer - if you are generating the forms on the basis of while loop, then you need to pas that $i
in every where in the form您的问题不清楚,但这是答案 - 如果您基于 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 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)
{
}
}
);
}
hope it will work for you希望它对你有用
You should add a class in form HTML node as below:您应该在表单 HTML 节点中添加一个类,如下所示:
<form class="anyclass" action="add your action where you want to post form">
And after this, you can submit form as below:在此之后,您可以提交如下表格:
$(".anyclass").submit(function(e) {
e.preventDefault
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: $(this).serialize(),
success: function(data)
{
alert('form submitted herer');
}
});
Hope it helps you.希望对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.