I have some some information being passed from PHP to javascript (not an AJAX call) to initialize some dynamic content.
on the server side I have
echo 'var ' . $controlID . '_json = JSON.parse(\'' . $control->getOptions() . '\');';
where $control->getOptions is
public function getOptions() {
//some code to build an array here
return json_encode($somearray);
}
which results in the following javascript code browser
var ControlName_json = JSON.parse('/*JSON OUTPUT HERE */');
Now, this generates an error for some reason. (Error, unexpected token a). I checked and the browsers I'm using do have JSON. However, this does work:
echo 'var ' . $controlID . '_json = ' . $control->getOptions() ';';
Is there anything wrong with directly assigning the variable as an object? Could that 'break' the javascript down the road somehow?
For completeness, the particular JSON causing the problem is below, however since it's created by json_encode I'm not sure it matters.
{"o0":[{"text":"aguapop","value":"aguapop","selected":false,"parentID":0,"attributes":" value=\"aguapop\""},{"text":"default","value":"default","selected":false,"parentID":0,"attributes":" value=\"default\""},{"text":"fluid","value":"fluid","selected":false,"parentID":0,"attributes":" value=\"fluid\""},{"text":"fresh","value":"fresh","selected":false,"parentID":0,"attributes":" value=\"fresh\""},{"text":"gel","value":"gel","selected":false,"parentID":0,"attributes":" value=\"gel\""},{"text":"professional","value":"professional","selected":false,"parentID":0,"attributes":" value=\"professional\""},{"text":"professional-rtl","value":"professional-rtl","selected":false,"parentID":0,"attributes":" value=\"professional-rtl\""},{"text":"silverwolf","value":"silverwolf","selected":false,"parentID":0,"attributes":" value=\"silverwolf\""},{"text":"wood","value":"wood","selected":false,"parentID":0,"attributes":" value=\"wood\""}]}