user1117313
user1117313

Reputation: 1985

Ajax get the error or success message from PHP function

I'm post data to a php function using JSON post. I want to receive two different type of responses, success and error. For example, the following function:

function some_function(){

    if ($error){
        echo "some error";
        exit;
    } else {    
        echo "everything is ok"
        exit;
    }

}

In this case, both messages will be returned as success. How can I make the error message return to the error function in the ajax? Here's the ajax code:

jQuery.ajax({

    url: '<?php echo $url; ?>',
    data: response,
    dataType: 'JSON',
    type: 'POST',
    success:function(data){
        console.log(data);
    },
    error: function(data, errorThrown){
        console.log(errorThrown);
    }

});

Upvotes: 1

Views: 10330

Answers (4)

Simone
Simone

Reputation: 166

I don't have enough reputation to comment, but as of JQuery 1.5 you'll need to use

error: function(data) {
    console.log(data['responseText']);
}

to get the json_encoded array your PHP is returning.

Upvotes: -1

Elangovan
Elangovan

Reputation: 3558

Thanks.The below JSON example is working fine.pls find it.

jQuery.ajax({

url: '<?php echo $url; ?>',
data: response,
dataType: 'JSON',
type: 'POST',
success: response_function
});

response_function(response){
 if(response.status == 1){
     //success response
     alert(response.message);
 }else{
      //failure response 
      alert(response.message);
      return false;
 }
 }

On the php script page

 function some_function(){

    if ($error){
        $msg = array("status"=> '1',    "message"=>"success message");
    } else {    
        array("status"=> '0', "message"=>"some error");
    }
   echo json_encode($msg);die;
}

Elangovan

Upvotes: 0

user1978142
user1978142

Reputation: 7948

If you want to force it to go to the error callback, then just force a wrong response from the server. Consider this example:

<?php

$url = 'index.php'; // sample value

function response($value) {
    if($value == 'true') {
        http_response_code(200);
        $data = array('status' => 200, 'message' => 'Server response is okay!');
        echo json_encode($data);
    } else {
        http_response_code(404);
    }
    exit;
}

if(isset($_POST['response'])) {
    $response = $_POST['response'];
    response($response);
}

?>


<script src="jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){

    jQuery.ajax({

        url: '<?php echo $url; ?>',
        data: {response: true}, // <-- set to TRUE or FALSE 
        dataType: 'JSON',
        type: 'POST',
        success:function(data){
            console.log(data);
        },
        error: function(data, errorThrown){
            console.log('error => ' + errorThrown);
        }

    });

});
</script>

Upvotes: 3

Jitendra Yadav
Jitendra Yadav

Reputation: 896

Try this one:

jQuery.ajax({

    url: '<?php echo $url; ?>',
    data: response,
    dataType: 'JSON',
    type: 'POST',
    success:function(data){
        if(data['error']){
           alert(data['error']);
          }else{
             alert(data['success']);
          }
    },
    error: function(data, errorThrown){
        console.log(errorThrown);
    }

});

On PHP page:

function some_function(){

        if ($error){
            $msg = array("error"=>"some error";
        } else {    
            $msg = array("success"=>"everything is ok");
        }
       echo json_encode($msg);die;
    }

Upvotes: 3

Related Questions