Pass A Variable From PHP To JavaScript
Solution 1:
In your PHP, you probably want to use json_encode
for encoding all of your data:
$result = array(
'data' => json_encode($settings)
);
echo json_encode($result);
Second, in your javascript, eval
is rarely (never) a good idea. From Douglas Crockford's style guide:
The eval function is the most misused feature of JavaScript. Avoid it.
Instead, you probably want to use JSON.parse()
to rebuild the result returned by the server:
console.log(result) ;
var resultObj = JSON.parse(result);
console.log(resultObj);
If your code is still broken, you might want to double-check your PHP to make sure it isn't spewing out any output beyond the json_encode
statement.
Solution 2:
Something is outputting a "1" in front of your json string. Javascript is unable to resolve that properly during eval().
Solution 3:
Well that line of code isn't valid.
Write in the console:
'(1{"data":[{"id":"1","alert_type_id":"1","deviation":null,"threshold_low":"20","threshold_high":"80"}]})'
As this is what you're passing to the eval
function. I should give you the same error.
If you want to save that string in a variable you need to adjust it a bit:
eval('var x =' + result);
Anyway It looks like you're doing something wrong, check again why do you need ti use the "evil" eval
.
Post a Comment for "Pass A Variable From PHP To JavaScript"