How to return a ajax error?

I want to show a ajax error after submitting a form. It ends now with 'die' but what is the best way to handle this? Just write something in this php file in 'script' tags?

if($_POST['postForm'] == 'newsletter'){

    $newsletterSubscriber = new NewsletterSubscriber();
    $newsletterSubscriber->set('CMS_newsletters_id', 2);
    $newsletterSubscriber->set('created', date('Y-m-d H:i:s'));
    $newsletterSubscriber->set('firstName', $_POST['voornaam']);
    $newsletterSubscriber->set('lastName', $_POST['achternaam']);
    $newsletterSubscriber->set('companyName', $_POST['beddrijfsnaam']);
    $newsletterSubscriber->set('emailAddress', $_POST['email']);
    $newsletterSubscriber->set('subscribed', 1);
    $saved = $newsletterSubscriber->save();

    die('subscriber added');
}

I tried several solutions I found but I can't get it to work.

Thanks!

Upvotes: 2

Views: 69

Answers (5)

RiggsFolly
RiggsFolly

Reputation: 94662

All you need to do is create a array and place any parameters you want to pass back into that array, then use json_encode() to turn it into a json string that can be easily processed by javascript

if($_POST['postForm'] == 'newsletter'){

    $newsletterSubscriber = new NewsletterSubscriber();
    $newsletterSubscriber->set('CMS_newsletters_id', 2);
    $newsletterSubscriber->set('created', date('Y-m-d H:i:s'));
    $newsletterSubscriber->set('firstName', $_POST['voornaam']);
    $newsletterSubscriber->set('lastName', $_POST['achternaam']);
    $newsletterSubscriber->set('companyName', $_POST['beddrijfsnaam']);
    $newsletterSubscriber->set('emailAddress', $_POST['email']);
    $newsletterSubscriber->set('subscribed', 1);
    $saved = $newsletterSubscriber->save();

    $response = array('error_code'=>0, 
                      'message'=>'subscriber added'
                     );
    echo json_encode($response);
    exit;
}

The javascript woudl be something like

$.ajax({
    type: "POST",
    url: "connection.php",
    data: {param1: 'aaa'},
    dataType: JSON
})
.done( function(data){
    if(data.error_code == 0) {
       alert(data.message);
    }
});

Note when you use dataType:JSON the browser automatically converts the json string returned to a javascript object so you can address data.error_code and data.message in simple javascript object notation

Upvotes: 1

madalinivascu
madalinivascu

Reputation: 32354

Use a json message followed by a error number:

if($saved) {
echo json_encode(array('message'=>'Successfully saved','erno'=>0));
} else {
echo json_encode(array('message'=>'Error on save','erno'=>1));
}

js:

success:function(data) {
  if(data.erno == 1) {
   alert(data.message)
   //do other stuf here
   } else {
    alert(data.message)//if save was successful 
   }
}

Upvotes: 1

Nishanth Matha
Nishanth Matha

Reputation: 6081

the best solution is to make you custom json and send it to ajax:

instead of die try:

$message = array('error'=>'subscriber added');
 echo json_encode($message);

and in you ajax callback do:

function(success) {
    if(success.error) {
        //do stuff
    }
//do stff
}

Upvotes: 1

Mayday
Mayday

Reputation: 5136

Did you check jQuery Ajax API? this comes directly from their example. It says that you can use the .done() .fail and .always() functions

var jqxhr = $.ajax( "example.php" )
  .done(function() {
  alert( "success" );
})
.fail(function() {
  alert( "error" );
})
.always(function() {
  alert( "complete" );
});

Upvotes: 1

Dhara Parmar
Dhara Parmar

Reputation: 8101

You can do like:

if($saved) {
die('subscriber added');
} else {
echo "error";
}

and In ajax you can check:

$.ajax({
    type: "POST",
    url: "savedata.php",
    data: form,
    cache: false,
    success:  function(data){
        if(data == "error") {
           alert("Data has not been saved successfully. Please try again.");
           window.location.reload(true);
        }
    }
});

Upvotes: 1

Related Questions