0

I am using AJAX to call a PHP script. I am using conditions to echo the proper error message in my PHP. When I do this, my AJAX and JQUERY do not work properly.

My JQUERY/AJAX:

        if (email != 0) {
            // Run AJAX email validation and check to see if the email is already taken
            $.ajax({  
                type: "POST",  
                url: "checkemail.php",  
                data: dataString,
                async: false,
                success: function(data) {
                    var error= false;

                    if (data == 'invalid') {
                        var invalid= 1;
                    }
                    else if (data == 'taken') {
                        var taken= 1;
                    }
                    if (invalid == 1) {
                        alert('invalid email');
                        e.preventDefault();
                    }
                    if (taken == 1) {
                        alert('email taken');
                        e.preventDefault();
                    }
                }
            });
        }

My PHP:

<?php

$email = true

if ($email == true) {

    echo "taken";

}


?>

But, when I just put:

echo "taken";

The AJAX and JQUERY works exactly how it should and the respective error message pops up. "taken" is being echo'd either way, so I don't get what is going on. What could I be doing wrong?

3
  • 1
    You should test your PHP code before making ajax request to them. Commented Jan 16, 2014 at 15:35
  • 1
    This is you I suppose Commented Jan 16, 2014 at 15:35
  • As of jQuery 1.8, the use of async:false in jQuery.ajax() is deprecated. Commented Jan 16, 2014 at 15:39

1 Answer 1

3

You're missing your semicolon.

$email = true

needs to be

$email = true;

In your response, you will probably be getting a PHP error - unless your error messages are suppressed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.