2

I have a form submit event that calls a WordPress ajax function. The form submit event occurs only once (It is correct as expected). But the ajax function loaded TWICE (expected to occur once).

Here is a sample of my code.

#Jquery

$('#my_form').submit(function () {
        console.log("This log print ONCE");
        var name = $('#name').val();
        var email = $('#email').val();
        $.ajax({
            type: 'post',
            url: '/wp-admin/admin-ajax.php',
            data: {
               action: 'my_ajax_function',
               name: name,
               email: email,
            },
            dataType: "json",
            success: function (data) {
                $('#response').html(data.message);
            }
        });
        return false; //  to prevent default form submit;
    });

#WordPress functions.php

<?php
function my_ajax_function() {
    custom_log("This log print TWICE"); 

    $name = (isset($_POST["name"])) ? $_POST["name"] : '';
    $email = (isset($_POST['email'])) ? $_POST['email'] : '';

    // call to a function to encrypt data
    encrypt_data($name, $email);
  
    // send response back to ajax
    $result['type'] = "success";
    $result['message'] = "Email sent successfully";
    json_encode($result);
    echo $result;
    die();
}

add_action('wp_ajax_nopriv_my_ajax_function', 'my_ajax_function'); 
add_action('wp_ajax_my_ajax_function', 'my_ajax_function');
?>

Ajax functionality is worked. But the "encrypt_data" function called twice. (I have created a log function custom_log in my_ajax_function and it also get printed twice).

Nb: I have checked the Network tab in inspect mode. The my_ajax_function action called only once in network tab.

Why the ajax function loading multiple times (twice) ? Is there anything I am missing?

4
  • Given that your callback terminates with die(), it shouldn't be possible for this function to execute twice in the course of serving a single request. Consider checking your web server's access logs to see if it's serving one request per form submission, or two. If you're positive that your JS is only executing one request, it's possible that some server or intermediary configuration is responsible.
    – bosco
    Apr 25, 2022 at 21:41
  • I have tested with multiple sites and servers. Issue is still there. But I could find that the problem occurs when ajax process takes more than 1 minutes. After 1 minute of running ajax request it execute second request. (Nb: In small sites where the ajax completed less than 1 minute have no issues.)
    – Irfan
    Apr 26, 2022 at 7:15
  • Does the JavaScript actually send a second request after 1 minute? If so that seems like an issue with the jQuery AJAX function, not anything to do with WordPress. Apr 26, 2022 at 8:22
  • @JacobPeattie I have reviewed network tab. It is sending only a single AJAX request
    – Irfan
    Apr 26, 2022 at 8:41

0

Your Answer

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

Browse other questions tagged or ask your own question.