admin管理员组文章数量:1125585
I have 2 forms on my page that I need to submit and stay on the same page. The first form works fine, but the second form will go to the admin-ajax.php page.
First form/jQuery and Ajax request Like I said, this portion works fine.
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="commentform" class="commentform">
<textarea name="comment" id="comment" class="commentbox" rows="5" cols="150" required></textarea><br />
<input type="hidden" name="eventid" id="eventid" value="<?php echo $id; ?>" />
<input type="hidden" name="userid" id="userid" value="<?php echo $userid; ?>" />
<input type="hidden" name="action" value="commentform">
<button class="commentbtn">Add Comment</button>
</form>
$('mentform').submit(function (e) {
e.preventDefault();
var commentform = $('mentform');
$.ajax({
url: commentform.attr('action'),
data: commentform.serialize(), // form data
type: commentform.attr('method'), // POST
beforeSend: function (xhr) {
commentform.find('button').text('Processing...');
},
success: function (data) {
commentform.find('button').text('Add Comment');
$("mentform")[0].reset();
$('#response').html(data); // insert data
}
});
return false;
});
First form/jQuery and Ajax request This form will take me to the admin-ajax.php page. (it does at least process the data, just not staying on the current page)
<form action=".php" method="POST" id="replyform19" class="replyform">
<textarea name="reply" class="commentbox" rows="5" cols="150"></textarea>
<input name="eventid" type="hidden" value="1"><input name="userid" type="hidden" value="1">
<input name="parentid" type="hidden" value="19"><input name="action" type="hidden" value="replyform">
<button class="newreplybtn" id="newreplybtn19">Reply</button>
<button class="cancelbtn" id="cancelbtn19" value="19">Cancel</button>
</form>
$('.replyform').submit(function (e) {
e.preventDefault();
var replyform = $('.replyform');
$.ajax({
url: replyform.attr('action'),
data: replyform.serialize(), // form data
type: replyform.attr('method'), // POST
beforeSend: function (xhr) {
replyform.find('button').text('Processing...');
},
success: function (data) {
$('#response').html(data); // insert data
}
});
return false;
});
I have 2 forms on my page that I need to submit and stay on the same page. The first form works fine, but the second form will go to the admin-ajax.php page.
First form/jQuery and Ajax request Like I said, this portion works fine.
<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="commentform" class="commentform">
<textarea name="comment" id="comment" class="commentbox" rows="5" cols="150" required></textarea><br />
<input type="hidden" name="eventid" id="eventid" value="<?php echo $id; ?>" />
<input type="hidden" name="userid" id="userid" value="<?php echo $userid; ?>" />
<input type="hidden" name="action" value="commentform">
<button class="commentbtn">Add Comment</button>
</form>
$('.commentform').submit(function (e) {
e.preventDefault();
var commentform = $('.commentform');
$.ajax({
url: commentform.attr('action'),
data: commentform.serialize(), // form data
type: commentform.attr('method'), // POST
beforeSend: function (xhr) {
commentform.find('button').text('Processing...');
},
success: function (data) {
commentform.find('button').text('Add Comment');
$(".commentform")[0].reset();
$('#response').html(data); // insert data
}
});
return false;
});
First form/jQuery and Ajax request This form will take me to the admin-ajax.php page. (it does at least process the data, just not staying on the current page)
<form action="http://intranet.cheshire-med.com/generalinfo/communitybb/wp-admin/admin-ajax.php" method="POST" id="replyform19" class="replyform">
<textarea name="reply" class="commentbox" rows="5" cols="150"></textarea>
<input name="eventid" type="hidden" value="1"><input name="userid" type="hidden" value="1">
<input name="parentid" type="hidden" value="19"><input name="action" type="hidden" value="replyform">
<button class="newreplybtn" id="newreplybtn19">Reply</button>
<button class="cancelbtn" id="cancelbtn19" value="19">Cancel</button>
</form>
$('.replyform').submit(function (e) {
e.preventDefault();
var replyform = $('.replyform');
$.ajax({
url: replyform.attr('action'),
data: replyform.serialize(), // form data
type: replyform.attr('method'), // POST
beforeSend: function (xhr) {
replyform.find('button').text('Processing...');
},
success: function (data) {
$('#response').html(data); // insert data
}
});
return false;
});
Share
Improve this question
asked Feb 1, 2024 at 15:43
Darth Mikey DDarth Mikey D
931 silver badge9 bronze badges
1 Answer
Reset to default 0I figured it out. I was creating the form when a button was clicked. I had to put the replyform submit within that function. Working now.
本文标签: Help with jqueryajax requests
版权声明:本文标题:Help with jqueryajax requests 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736666586a1946695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论