HTML is NULL in JSON object from json_encode

Viewed 4093

I have a ajax call calling a php file who runs a long php function which returns a JSON encoded array/object. Now I need to send HTML also into the ajax response. I thought about sending the HTML inside the array.

Is that a good practise?

Right now I cannot get it working, I get a NULL as value for that property. Don't know why.

$statHTML = '<table>';
foreach ($toHTML as $key=>$value) {
    $statHTML.= '
        <tr class="'.$key.'">
            <td class="side">'.$value[0].'</td>
            <td>'.$value[2].' '.$value[1].'</td>
        </tr>';
}
$statHTML.= '</table>';
//  echo $statHTML;   // - this works
//function return   
$answer = array('mostSearched'=>$mostSearched,
                'timeOfDay' => $timeOfDay,
                'mostSearchedDays'=>$mostSearchedDays,
                'statHTML' => $statHTML                 
            );
return json_encode($answer);

The ajax response from the console before the JSON.parse():

{
    "mostSearched": {
        "title": "Most serached houses",
        "colNames": [21],
        "rowNames": [2013],
        "rows": [1]
    },
    "timeOfDay": {
        "title": "Time of search",
        "colNames": ["07:30"],
        "rowNames": ["07:30"],
        "rows": [
            [1]
        ]
    },
    "mostSearchedDays": {
        "title": "Most searched days",
        "colNames": ["2013-12-21", "2013-12-22", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26", "2013-12-27"],
        "rowNames": ["2013-12-21", "2013-12-22", "2013-12-23", "2013-12-24", "2013-12-25", "2013-12-26", "2013-12-27"],
        "rows": [
            [1, 1, 1, 1, 1, 1, 1]
        ]
    },
    "statHTML": null
}
3 Answers
Related