Ajax function issue on return true and false in wordpress

I am validating a form with ajax and jquery in WordPress post comments textarea for regex. But there is an issue when i want to alert a error message with return false. Its working fine with invalid data and showing alert and is not submitting. But when i put valid data then form is not submit. May be issue with return false.

I tried making variable and store true & false and apply condition out the ajax success block but did not work for me.

Read More

Its working fine when i do it with core php, ajax, jquery but not working in WordPress .

Here is my ajax, jquery 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 i’m using wordpress wp_ajax hook.

And here is my php code.

    <?php
function nmp_process_func (){
$comment = $_REQUEST['comment'];
preg_match_all("/(->|;|=|<|>|{|})/", $comment, $matches, PREG_SET_ORDER);
$count = 0;
foreach ($matches as $val) {
    $count++;
}
echo $count;
wp_die();
}
?> 

Thanks in advance.

Related posts

3 comments

  1. 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.

  2. 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!

  3. 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.

Comments are closed.