Edward
Edward

Reputation: 1883

Multiple JSON arrays as reponses - AJAX & PHP

I have a PHP script setup that echo's JSON responses depending on what the user has done (or not done as the case may be):

The responses look like this:

{"type":"error","response":"Script error, please reload the page and try again.
Code: [NAct]","title":"Exception","hide":false}

Each response is generated like this:

echo $form -> ajax_response('error', 'Script error, please reload the page and try again.<br>Code: [NAct]', 'Exception', false);

This is picked up by pNotify and displayed - lovely. (See below .done function for ajax request)

request.done(function(msg) {
    //validate json response
    if (!tryParseJSON(msg)) {
        document.write(msg);
    } else {
        var array = json_to_array(msg);
    }
    if (array['type'] !== 'none') {
        if (array['title'] !== null) {
            pushNotification(array['title'], array['response'], array['type'], array['hide']);
        } else {
            pushNotification(ucfirst(array['type']), array['response'], array['type'], array['hide']);
        }
    }
    ready_status();
});

If the response cannot be validated by tryParseJSON(); the reponse is written directly to the page for debugging.

The problem is when I echo multiple responses back like this:

{"type":"error","response":"Script error, please reload the page and try again.
Code: [NAct]","title":"Exception","hide":false}

{"type":"error","response":"Script error, please reload the page and try again.
Code: [NDat]","title":"Exception","hide":false}

tryParseJSON() sees it as mumbo jumbo and prints it to the page.

Question

How do i pick up the above two lines as separate responses and parse them through my function and sub-sequentially to pNotify without combining them into a single JSON array?

Solution

As pointed out this was over complicated. Instead I combined each response (PHP side) into a an array:

$res['json'][] = $form -> ajax_response('error', 'Script error, please reload the page and try again.<br>Code: [NAct]', 'Exception', false);

Then echo'ed it at the end of the script:

echo json_encode($res['json');

On client side, I used a for loop, sending them to pNotify in each iteration:

request.done(function(msg) {
    //validate json response
    if (!tryParseJSON(msg)) {
        document.write(msg);
    } else {
        var obj = json_to_array(msg);
    }
    for (var i=0;i<obj.length;i++) {
        if (obj[i]['type'] !== 'none') {
            if (obj[i]['title'] !== null) {
                pushNotification(obj[i]['title'], obj[i]['response'], obj[i]['type'], obj[i]['hide']);
            } else {
                pushNotification(ucfirst(obj[i]['type']), obj[i]['response'], obj[i]['type'], obj[i]['hide']);
            }
        }
    }
    ready_status();
});

Upvotes: 0

Views: 655

Answers (1)

newBee
newBee

Reputation: 1319

Instead of creating so sperate JSON-Outputs merge it to one single output string.

For this just wrap your two arrays you are currently outputting separately in an array like so

$myOutput[0] = responseArray1;
$myOutput[1] = responseArray2;
echo json_encode($myOutput);

This way you will get a valid JSON-response. Everything else is just some dirty workaround and causes shivers to everyone who has to review your work.

Upvotes: 1

Related Questions