Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass php variable value to javascript [duplicate]

Tags:

javascript

php

Im trying to pass the value of a php variable into javascript but i just cant get it to work. Am I doing anything wrong? Below is the line of code I'm working with.

var dist = parseInt("<?php echo json_encode($distance); ?>");
like image 832
cclerv Avatar asked Jul 23 '26 14:07

cclerv


2 Answers

$distance is an integer? why don't you just write

var dist = <?php echo $distance; ?>
like image 88
Sebastian Breit Avatar answered Jul 25 '26 03:07

Sebastian Breit


If the value in $distance is just an integer, you don't need the json_encode call. You can just do a php echo of $distance.

Something like

var dist = <?php echo $distance; ?>;
like image 38
Lucas Holt Avatar answered Jul 25 '26 03:07

Lucas Holt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!