ajax multiple,jquery - AJAX multiple form post - Stack Overflow

博主求助:使用AJAX在页面上实现多个表单提交删除数据库对应用户的功能,但只有顶部表单有效。寻求解决AJAX函数只针对第一个表单生效的问题,可能涉及动态加载或事件监听问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have a basic AJAX form submission function in the head of my page (stolen from a tutorial because I can't write this stuff from scratch). In the body of my page I have a bunch of forms with hidden inputs that have values corresponding to user id's that are in my database.

The idea is that when you submit a form, the hidden value is POST'd to my PHP page where it deletes the user with that id from my MySQL database. It's all pretty straight forward and there's probably a much more efficient way to do it but this is the only way I could think of.

Anyway the problem is my AJAX function only works with the first form in my page. When I submit the form, the user is deleted from my database, the page refreshes and that particular form disappears. But if you click any form beneath the top one, nothing happens. Here's my jQuery function:

$(document).ready(function(){

$("#myform").validate({

debug: false,

rules: {

group: {required: true},

},

messages: {

group: {required: " Field required.", loginRegex: " Invalid character."},

},

submitHandler: function(form) {

$.post('process_delete_participant.php', $("#myform").serialize(), function(data) {

$('#results').html(data);

});

}

});

});

And here's what's in the body of my page:

$query = mysql_query('SELECT * FROM `Participant`');

if (!$query)

{

die('Query failure: '.mysql_error());

}

while($rows = mysql_fetch_array($query))

{

$fname = $rows['firstName'];

$sname = $rows['secondName'];

$id = $rows['idParticipant'];

echo "

";

echo "";

echo "$fname ";

echo "$sname ";

echo " ";

echo "

";

echo "
";

}

?>

So an example of one of the forms could look something like this:

John Smith

I can think of a couple of reasons why the function only works with the topmost form but I have no idea what to edit in the syntax of my function to solve the issue. Anyone know?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值