tags:

views:

74

answers:

2

Hello everyone,

When i do an ajax request i go to a php script which does something for me. Either it deletes an record from the db or it doesn't.

If it goes ok. I want to deleted and update my html table. Else I want to show an error message or so.

But in my php I can't do something like

return false;  
return true;

I need to echo my result or my message and then my jquery script will do something with it.

But how can I get jquery to do something depending on the outcome?

For example here:

jQuery.ajax({
   type: "POST",
   data: "id=" +id,
   url: "ajax_handler.php",
   success: function(msg){
    jQuery(tr).remove();
    jQuery("div.result").append("<p>"+msg+"</p>");
    jQuery("div.result").show();
    jQuery("div.result").fadeOut(5000);

   },
   error: function(msg){

   }

and the php code

$id = C_GPC::getPOSTvar("id");
$result ="";
if(isset($id) and ($id > 0)){
    $p = new C_product();
    $deleted = $p->deleteCategorie($id);
    if($deleted){
     $result = "ok";
    }else{
     $result = "not ok";
    }
}else{

    $result = "not ok";
}
echo $result;

I want to do A when ok and B when not ok. How should i deal with this? });

+1  A: 

msg (the success result param) will return any output from the server/PHP script. So for your PHP script, if it doesn't delete the record, then you could echo out an error message. In your Javascript code you can then check:

if(msg == 'not ok')
{
    alert('error occured');
}

if(msg == 'ok')
    alert('update the table');
}

You could set up something similar for your table/update.

A: 

You could use the jQuery get function:

Have a look at the bottom example (under the Examples tab). You can handle the result from the request and decide what to do with it.

IllusivePro