javascript - How to receive a ajax callback in ajax post -
i using ajax codeigniter application. i've manged make "ajax checkbox", when click on checkbox, in background specific function called. how make callback, mean know if operation done ok, or maybe problem/error.
my html:
<input type="checkbox" onclick="change_parameter(<?=$dane_leada['lead_id']?>, 'my_parameter');" >
js:
function change_parameter(lead_id, parametr) { $.ajax({ type : "post", url : '<?=base_url();?>leads/change_parametr/' + lead_id, data : "lead_id=" + lead_id, data : "parameter=" + parameter, }); alert("status changed"); //here should message "ok" or "error" }
php in controller:
public function change_parameter($lead_id, $parametr=false) { if ($lead_id != "" , isset($_post['parameter'])) { $parameter = $_post['parameter']; //odczytujemy zmienną wysłaną przez ajaxa } if ($data['dane_leada'][$parameter] == '0') $new_parameter = 1; else $new_parameter = 0; $dane = array( 'lead_id' => $lead_id, $parameter => $new_parameter, ); $this->model_leady->mofify_lead($lead_id, $dane); // return error or confirm }
update script this:
function change_parameter(lead_id, parametr) { $.ajax({ type : "post", url : '<?=base_url();?>leads/change_parametr/' + lead_id, data : {lead_id : lead_id,parameter :parameter}, success: function(response){ if(response){ alert("status changed"); //here should message "ok" or "error" }else{ alert("error :something wrong"); } }, error: function (err) { alert("error :something wrong"); }); }); }
or can add error handler.
error: function (err) { alert("error :" + err.status); }
Comments
Post a Comment