1

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\""}]}
2
  • I didn't see a json_decode() function in your php. But I see json.parse() which is not how to decode a JSON object in php. Maybe I am just misreading? Commented Mar 19, 2014 at 6:16
  • I don't need to decode the json in PHP. PHP is sending an associative array to javascript, but JSON.parse() is throwing an error on the output. Do I need to use JSON.parse() or is it fine to send it natively? Commented Mar 19, 2014 at 6:25

3 Answers 3

1

In JS there is no need to parse the JSON, just assign it directly to a variable. Worked for me on JSfiddle with your JSON example:

echo 'var ' . $controlID . '_json = '. $control->getOptions() . ';';

Throw in the JSON.parse and it fails on the unexpected token.

Sign up to request clarification or add additional context in comments.

1 Comment

are there any possible problems from doing things this way?
1

The problem is in parsing the "attributes" attribute, which non of them are a valid JSON value, for instance you've got:

"attributes":" value=\"wood\"

in your json string, when I changed it to:

"attributes":" value='wood'"

or

"attributes":" value=\'wood\'"

the problem got solved.

The other way around is not to use JSON.parse, although the "attributes" values in your json are not valid to get parsed in JSON.parse, but it could be a valid JavaScript Object, so you can do it like:

echo 'var ' . $controlID . '_json = ' . $control->getOptions() . ';';

4 Comments

the JSON is being passed directly from json_encode, is there any way to control the output of json_encode?
@serakfalcon: you could modify it manually using string functions, in php or JavaScript, but I don't think it is a good practice.
@serakfalcon: somewhere in your code before it gets encoded you pass the attributes value to the object, why not passing for instance the actual value like: "attributes":"wood" instead of "attributes":" value=\"wood\".
ah, the attributes is a arbitrary string of html attributes to be assigned to an element inside the tag of that element, in this case it happens to be value, but it could also be name, or style or any other html attribute or combination thereof.
1

You should be encoding the JSON as the last step. Run this simple example, and check your javascript console.

<?php
$cars = array
(
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
//print_r($cars);
?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>JSON Parse</title>
<script>
var output = '<?php echo json_encode($cars); ?>';
console.log(JSON.parse(output));
</script>
</head>
<body>
</body>
</html>

1 Comment

encoding within the class function is safer if I'm planning on passing the output directly to the browser-- that way I don't have remember to encode the output every time I call that function, and it shouldn't matter when I encode the function. It also doesn't fix the real problem, which is one of the array values is something like 'value="test" id="StyleID"' which apparently json_encode doesn't treat properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.