I am creating an events plugin with a commenting section for each event. I am able to save each comment, but the problem I'm having now is that I am taken to admin-ajax.php which is blank. When the form is submitted, I want to stay on the same page and have results loaded. My code is below.


<form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="comment_form">
    <textarea name="comment" id="comment" class="commentbox" rows="5" cols="120" 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>
<div id="response"></div> 


        var commentform = $('#commentform');
            data:commentform.serialize(), // form data
            type:commentform.attr('method'), // POST
                commentform.find('button').text('Apply filter');
                $('#response').html(data); // insert data
        return false;

PHP Function

add_action('wp_ajax_commentform', 'comment_submit'); 
add_action('wp_ajax_nopriv_commentform', 'comment_submit'); 
function comment_submit() { 
    global $wpdb; 
    $eventid = $_POST["eventid"];
    $userid = $_POST["userid"]; 
    $comment = $_POST["comment"];
    $date = date('Y-m-d H:i:s');

    $inserttable = $wpdb->prefix . "event_comments";
    $data = array(
        'eventid' => $eventid,
        'userid' => $userid,
        'content' => $comment,
        'datesaved' => $date,
    $wpdb->insert($inserttable, $data);


3 Answers 3


The code you provided is on the right track, but it's possible that the issue you're facing is related to WordPress's admin-ajax.php handling.

Here's how you can modify your code to make sure you stay on the same page after submitting the form:

  1. Change the form action to the current page instead of admin-ajax.php.


<form action="<?php echo esc_url(admin_url('admin-ajax.php')); ?>" method="POST" id="comment_form">
  1. Modify your JavaScript
jQuery(function($) {
    $('#comment_form').submit(function(e) {
        e.preventDefault(); // Prevent the default form submission

        var commentform = $(this);
            url: commentform.attr('action'),
            data: commentform.serialize(), // Form data
            type: commentform.attr('method'), // POST
            beforeSend: function(xhr) {
            success: function(data) {
                commentform.find('button').text('Add Comment');
                $('#response').html(data); // Insert data
  1. Make sure your JavaScript code is enqueued properly in your WordPress theme or plugin.

  2. Ensure that the JavaScript code is loaded on the page where the form is present.

With these changes, your form should be submitted via AJAX, and the page will not be redirected to admin-ajax.php. Instead, the response will be loaded into the #response div on the same page.

  • Thanks for the response. I had actually already figured out what the issue was. I forgot to put in event.preventDefault. Commented Oct 31, 2023 at 11:58

I am taken to admin-ajax.php

That's what you are telling the form to do. Leave the action field blank and it will return to the same page.

Review the first User Note on this page for a proper example of how to set the ajax url field.

Or use this alternative approach.


I figured out the issue. I forgot to put event.preventDefault in the jQuery. It's all set now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.