I am using JSON to communicate with the user. PHP converts array to JSON to this form:
{"success":"text-to-display","warning":"NONE","notice":"text-to-display","error":"NONE"}
jQuery display notification:
function callback (data){
if(data.notice !== 'NONE'){
displayNotice(data.notice);
}
if(data.success !== 'NONE'){
displaySuccess(data.success);
}
if(data.warning !== 'NONE'){
displayWarning(data.warning);
}
if(data.error !== 'NONE'){
displayError(data.error);
}
}
Unfortunately, in this method can't display two error or two notice or two warning, because new statement replace old statement.
<?php
$uwaga['error'] = 'old statement';
$uwaga['error'] = 'new statement';
// display only "new statement"
echo json_encode($uwaga);
?>
I think that use array:
<?php
$uwaga = array();
$uwaga[1] = array('type' => 'notice', 'text' => 'old statement');
$uwaga[2] = array('type' => 'notice', 'text' => 'new statement');
// display "new statement" and "old statement"
// generate: {"1":{"type":"notice","text":"old statement"},"2": {"type":"notice","text":"new statement"}}
echo json_encode($uwaga);
?>
How "translate" this PHP code on jQuery (mainly: how convert json object to array? how loop use? how using this loop? How refer to $uwaga[$key]['name'] and $uwaga[$key]['text'])?
foreach ($uwaga as $key => $value) {
switch ($uwaga[$key]['name']) {
case 'warning':
displayWarning($uwaga[$key]['text']);
break;
}}