json - PHP json_encode errors messages for JQuery -
i have following method in php class processes messages , sends jquery. works fine if there single message send if there more one, sends them separate json objects. messages sent ok jquery gives me error. messages this:
{"error":true,"msg":"message 1 here..."}{"error":true,"msg":"message 2 here"} my php method looks this:
private function responsemessage($bool, $msg) { $return['error'] = $bool; $return['msg'] = $msg; if (isset($_post['plajax']) && $_post['plajax'] == true) { echo json_encode($return); } ... } i don't know how change multiple error messages put single json encoded message work if single message.
can help? thanks
looks need appending onto array , when messages have been added, output json. currently, function outputs json time called:
// array property hold messages private $messages = array(); // call $this->addmessage() add new messages private function addmessage($bool, $msg) { // append new message onto array $this->messages[] = array( 'error' => $bool, 'msg' => $msg ); } // finally, output responsemessage() dump out complete json. private function responsemessage() { if (isset($_post['plajax']) && $_post['plajax'] == true) { echo json_encode($this->messages); } ... } the output json array of objects resembling:
[{"error":true,"msg":"message 1 here..."},{"error":true,"msg":"message 2 here"}]
Comments
Post a Comment