Ajax function issue on return true and false in wordpress Ajax function issue on return true and false in wordpress wordpress wordpress

Ajax function issue on return true and false in wordpress


Finally, I just figured it out by myself.

Just put async: false in ajax call. And now it is working fine. Plus create an empty variable and store Boolean values in it and then after ajax call return that variable.

Here is my previous code:

    require 'nmp_process.php';add_action('wp_ajax_nmp_process_ajax', 'nmp_process_func');add_action('wp_ajax_nopriv_nmp_process_ajax', 'nmp_process_func');add_action('wp_head', 'no_markup');function no_markup() {    ?>    <script type="text/javascript">        jQuery(document).ready(function () {            jQuery('form').submit(function (e) {                var comment = jQuery('#comment').val();                jQuery.ajax({                    method: "POST",                    url: '<?php echo admin_url('admin-ajax.php'); ?>',                    data: 'action=nmp_process_ajax&comment=' + comment,                    success: function (res) {                        count = res;                        if (count > 10) {                            alert("Sorry You Can't Put Code Here.");                            return false;                        }                    }                });                return false;            });        });    </script><?php}

And the issue that i resolved is,

New code

var returnval = false;jQuery.ajax({           method: "POST",           url: '<?php echo admin_url('admin-ajax.php'); ?>',           async: false, // Add this           data: 'action=nmp_process_ajax&comment=' + comment,

Why i use it

Async:False will hold the execution of rest code. Once you get response of ajax, only then, rest of the code will execute.

And Then simply store Boolean in variable like this ,

success: function (res) {                        count = res;                        if (count > 10) {                            alert("Sorry You Can't Put Code Here.");                            returnval = false;                        } else {                            returnval = true;                        }                    }                });                // Prevent Default Submission Form                return returnval; });

That's it.

Thanks for the answers by the way.


Try doing a ajax call with a click event and if the fields are valid you submit the form:

jQuery(document).ready(function () {            jQuery("input[type=submit]").click(function (e) {                var form = $(this).closest('form');                e.preventDefault();                var comment = jQuery('#comment').val();                jQuery.ajax({                    method: "POST",                    url: '<?php echo admin_url('admin-ajax.php'); ?>',                    data: {'action':'nmp_process_ajax','comment':comment},                    success: function (res) {                       var count = parseInt(res);                        if (count > 10) {                            alert("Sorry You Can't Put Code Here.");                        } else {                              form.submit();                        }                    }                });            });        });

note : you call need to call that function in php and return only the count!


Instead of submitting the form bind the submit button to a click event.

jQuery("input[type=submit]").on("click",function(){   //ajax call here   var comment = jQuery('#comment').val();   jQuery.ajax({       method: "POST",       url: '<?php echo admin_url('admin-ajax.php'); ?>',       data: 'action=nmp_process_ajax&comment=' + comment,       success: function (res) {             count = res;              if (count > 10) {                  alert("Sorry You Can't Put Code Here.");                  return false;               }else{                  jQuery("form").submit();               }           }      });   return false;})

Plus also its a good idea to put return type to you ajax request.Let me know if this works.