I want to push a php variable to javascript. The code is below but does not seem to work. Can anyone show me how to do this?
ticks.push(<?php echo json_encode($new); ?>);
BRBT
1,4809 gold badges28 silver badges49 bronze badges
3 Answers 3
Is this what you're looking for?
ticks.push(JSON.parse('<?= json_encode($new); ?>'));
Or broken down:
var json = '<?= json_encode($new); ?>';
var obj = JSON.parse(json);
ticks.push(obj)
also addressed in this issue:
answered May 15, 2015 at 6:32
Sign up to request clarification or add additional context in comments.
Comments
can you try this
var tricks = [
<?php
foreach ($new as $n) {
echo '"'.$n'", ';
}
?>
];
1 Comment
Havelock
There's so much "wrong" with your suggestion, that I just don't know where to start from...
you can use websocket to do such like functionality; in it you can send the result back to the client from the php code when it is ready one good example on that is how stackoverflow notify you when there is new question, or when they notify you if the post is edited.
answered May 15, 2015 at 6:15
Comments
default
echo json_encode($new);
produce? What's the resulting line of javascript?