user1053263
user1053263

Reputation: 742

Find error PHP returns to AJAX in JQuery

this works:

 $.ajax({
            type: 'POST',
            url: 'register.php',
            data: {captcha: captcha
            },

            success: function() {
            $('#loading').hide();
            $('#success').fadeIn();
            } 
            error: function() {
            $('#loading').hide();
            $('#captcha').fadeIn();
            $('#catErrorB').fadeIn();
            } 

});     

sends captcha response to PHP and if entered correctly, you can register. problem is, is that if you incorectly enter your captcha, the JQuery still runs the functions for a successful run although the PHP ran a "die" and did nothing.

In PHP, if the captcha is entered incorrectly is does this

if (!$resp->is_valid) {
           die ("false");
  } 

  else 

  {
  register
  }

How do I request the error that PHP spits out so I can do something like?

    success: function(find error) {
        if(error == "false")
        {
        $('#loading').hide();
    $('#captcha').fadeIn();
    $('#catErrorB').fadeIn();
        }
        else
        {
        $('#loading').hide();
        $('#success').fadeIn();
        }
        } 

EDIT!: With your help heres what it looks like now and it works fantastic!!

$.ajax({
            type: 'POST',
            url: 'register.php',
            dataType: "json",
            data: {
                challenge: challenge,
                response: response,
                zip: zip
            },

            success: function(result) {  
            if (!result.success) { 
            $('#loading').hide();
            $('#captcha').fadeIn();
            $('#catErrorB').fadeIn();
            } 
            else { 
            $('#loading').hide();
            $('#success').fadeIn();
    } 
} 




});         

and the PHP

if (!$resp->is_valid) {
    $response = array(success => false); 
    echo json_encode($response); 

  } 

  else 
  {

$response = array(success => true); 
echo json_encode($response); 

Ya'll are the coolest. My first JQuery I've ever done, It's came out so awesome. This site rules!

Upvotes: 0

Views: 765

Answers (2)

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

PHP:

if (!$resp->is_valid) {
    $response = array(success => false);
    echo json_encode($response);
}
else { 
    $response = array(success => true);
    echo json_encode($response);
}

jQuery:

success: function(result) { 
    if (!result.success) {
        // Wrong input submitted ..
    }
    else {
        // Correct input submitted ..
    }
}

Upvotes: 1

Barry Chapman
Barry Chapman

Reputation: 6780

I wouldn't use success/failure. Even if the script dies it is still going to return a 200 SUCCESS to your ajax call.

Use JSON to return responses depending on whether or not it is a success and parse and proceed with correct logic

<?php

if ( $resp->is_valid ) {
  echo json_encode( array( 'status' => 'true' ) );
} else {
  echo json_encode( array( 'status' => 'false' ) );
}

?>

Then you can parse the response in your AJAX call. Remember, if you call die('false'); that is still going to return a success message to your ajax function.

Upvotes: 2

Related Questions