I'm working on a method in my class that outputs error messages for both ajax and regular post request. The PHP part works fine, but the json part doesn't seem to. Here is my 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 as $key => $value) {
$this->phpErrors .= $msg;
}
}
$this->formError = true;
}
I think, the problem is that no matter if it is just a single error message or multiple, the json object is always wrapped with square brackets. I couldn't find anything online that would show an example. The messages look like 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 but my JQuery does not output any 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 I was showing only one message at a time, the JQuery was working fine.
Any help would be great. Thanks