Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 327
Shaban Avatar asked Nov 19 '15 07:11

Shaban


People also ask

Why is Ajax success not working?

ajax post method. The reason was my response was not in the JSON format so there was no need for the dataType: 'json' line in the submit method. In my case, the returned response was in text format that's why it was not going to success event. Solution: Remove dataType: 'json' line.

How can I tell if Wordpress Ajax is working?

To see if the current request is an AJAX request sent from a js library ( like jQuery ), you could try something like this: if( ! empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ]) == 'xmlhttprequest' ) { //This is an ajax request. }

What does AJAX request return?

It receives the returned data and the value of dataType , and must return the (possibly altered) data to pass on to success . success callback option is invoked, if the request succeeds. It receives the returned data, a string containing the success code, and the jqXHR object.


1 Answers

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.

like image 97
Shaban Avatar answered Sep 26 '22 03:09

Shaban