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
Add a comment  | 

1 Answer 1

Reset to default 0

I 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