PHP json_encode for JQuery -
i'm working on method in class outputs error messages both ajax , regular post request. php part works fine, json part doesn't seem to. here method:
public $formerror = false; public $phperrors = ''; public $jsonerrors = array(); // ------------------------------------------------------------ // error processing // ------------------------------------------------------------ private function responsemessage($bool, $msg) { $return['error'] = $bool; $return['msg'] = $msg; if (isset($_post['plajax']) && $_post['plajax'] == true) { $this->jsonerrors[] = $return; } else { foreach ((array) $msg $key => $value) { $this->phperrors .= $msg; } } $this->formerror = true; } i think, problem no matter if single error message or multiple, json object wrapped square brackets. couldn't find online show example. messages this:
single error:
[{"error":true,"msg":"error message 1 ..."}]
multiple errors:
[{"error":true,"msg":"error message 1 ..."},{"error":true,"msg":"error message 2 ..."}]
firebug shows 200 ok jquery not output messages:
$.ajax({ type: 'post', url: plsubmiturl, data: plformdata, datatype: 'json', cache: false, timeout: 10000, success: function (data) { if (data.error === true) { // display error message plresponse(data.msg, true); ... } else if (data.error === false) { // display success message plresponse(data.msg, true); ... } }, when showing 1 message @ time, jquery working fine.
any great. thanks
since multiple error contains more 1 index, might not picking data. check length more error.
$.ajax({ type: 'post', url: plsubmiturl, data: plformdata, datatype: 'json', cache: false, timeout: 10000, success: function (data) { var x = data.length; if(x == 1){ if (data.error === true) { // display error message plresponse(data.msg, true); ... } else if (data.error === false) { // display success message plresponse(data.msg, true); } }else{ jquery.each(data,function(key,val){ // whatever need } }
Comments
Post a Comment