0

I'm using the jQuery.validate library to check if a form field matches an array of data.

Here's the documentation for the remote method.

When it returns true, jQuery.validate displays a 1 in an error field, and blocks the form as if there's been an error. I get the same result even if I have nothing but echo true; in my PHP file. Every reference I can find to this suggests setting async:false, which I've done. The code below is accurate to my web page:

var unl_validator = jQuery('.unlimited_coupon_form').validate({
    rules: {
        coupon: {
            required: true,
            minlength: 4,
            remote: {
                url: "<?= get_template_directory_uri() ?>/inc/checkCoupon.php",
                async:false,
                data: {
                    coupon: function(){
                        return jQuery('#subscribe_coupon').val();
                    },
                },
                type: "post",

            }
        }
    },
    messages: {
        coupon: { required: "Please enter a valid coupon.", minlength: "Please enter a valid coupon.", remote: "This coupon code is not valid." }
    },
    onkeyup: false,
    onblur: true
});
Jodi Warren
  • 400
  • 4
  • 21
  • 1
    You have another problem in your code. `onblur: true` is not a valid option and should be removed entirely. It's called `onfocusout` and this is already the default behavior. You really only have three options: 1. leave out `onfocusout` and it remains functional as a default, 2. set `onfocusout` to `false` to disable it, or 3. Specify a custom function to over-ride the default `onfocusout` function. – Sparky Apr 02 '13 at 23:58
  • 1
    See this answer: http://stackoverflow.com/a/15104059/594235 ~ It's about the `onkeyup` option, but the same idea applies to the `onfocusout` option. – Sparky Apr 03 '13 at 00:04
  • Thank you for the catch and general information Sparky, it's much appreciated. – Jodi Warren Apr 03 '13 at 07:42

1 Answers1

1

Does the validation plugin know what to do with true being displayed? Doesn't sound like it. You probably need to display something else in PHP.

To properly output a server response, a simple string is not enough. In PHP you can use JSON as such:

$result = true;
echo json_encode($result);

or if you need to output specific values

$result = new stdClass();
$result->someField = true;
$result->otherField = false;
echo json_encode($result);
// the above will output {someField: 1, otherField: 0}; 
flavian
  • 28,161
  • 11
  • 65
  • 105