pass php variable value to javascript [duplicate] - php

This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
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); ?>");

$distance is an integer? why don't you just write
var dist = <?php echo $distance; ?>

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; ?>;

if you right click > view html source in your web browser, you would see for yourself that you have an extra set of quotes.
And, good for you for using json_encode() to output it as a string. That's an excellent way to safely output a value to javascript. although, if its an integer, theres no need here.

Related

jquery set background color with variable [duplicate]

This question already has answers here:
What's the best way to pass a PHP variable to Javascript? [duplicate]
(4 answers)
Closed 7 years ago.
I want to set background color based on user preference that's in a database.
I set a variable named $bc from the session variable I saved from the row.
If I echo the either the session variable ($_SESSION['backcolr']) or the new variable ($bc), the correct value of yellow appears. However, if I try to use either variable with jQuery, it doesn't work. Not sure what I'm doing wrong.
<script type="text/javascript">
// $('#body').css(background-color','yellow') -- this works
$("body").css("background-color",'$bc'); --- this doesn't work
</script>
Hello you should modify your code like below;
<script type="text/javascript">
// $('#body').css(background-color','yellow') -- this works
$("body").css("background-color",'<?PHP echo $bc; ?>');
</script>
Thats because you need to tell PHP when it should be parsing PHP code.
Replace that line with:
$("body").css("background-color",'<?php echo $bc; ?>');

Getting data with php in a javascript file [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Echo PHP variable from JavaScript?
How can I get a PHP variable to AJAX?
I'm trying to get some data from my database with php, and put it in a javascript variable. what is the easiest way to do that using JQuery ?
This is easiest for me, where $var is your array in PHP, being set to the variable "a" in Javascript:
<script type="text/javascript">
var a = <?php echo json_encode($var); ?>;
</script>

Is there other ways of passing a php $_GET variable from url to a js variable? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Get query string values in JavaScript
I know you could do this by echoing the variable $_GET['whatevervariable'] to a js variable.i was just wondering if there are other methods that can also do this?
This will provide the entire $_GET array to your JavaScript app:
<script type="text/javascript"><!--
_GET = <?php echo json_encode($_GET); ?>;
--></script>
The $_GET superglobal is just an array of the querystring.
You can fetch the querystring in javascript with window.location.search, but to use it like $_GET will need some sort of parsing and usually a few regular expressions to handle difficult characters etc.
Just try var qs = window.location.search; and then figure out exactly what you need, and how you will get it.
The easy solution if doing it inline is just echoing $_GET into a variable, like in danorton's answer.

Using a Javascript variable inside a php block [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
Access a JavaScript variable from PHP
I have a Javascript function that takes in one variable. The function has some php code inside it. I want to use this variable inside the php section of the function. I couldn't get it to work. How is it done?
The issue is that your PHP code is being rendered on the server before being served to the client. I would recommend either converting the PHP code into Javascript code or creating an AJAX call to the PHP function.
Start reading about AJAX! You will likely need to rewrite some of the code you have written but what you are attempting to accomplish is not really possible otherwise.
try with it
<script>
var whatever = "<?php echo $phpVar ?>";
</script>

Render a PHP code using jQuery [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Pass a PHP string to a Javascript variable (including escaping newlines)
I got this problem rendering or making a PHP code functional using jQuery. huhu.
Can someone help me?
Here's the code I wanted to do:
document.getElementById("fade").innerHTML = '<?php echo 'SOME-PHP-CODE-HERE'; ?>
Try using
document.getElementById("fade").innerHTML = '<?php echo $var; ?>';
Check if $var has no quotes. If it has, you need to escape them using addSlashes()
And if you are using JQuery you can better write
$('#fade').html('<?php echo $var; ?>');
Try:
document.getElementById("fade").innerHTML = '<?=$php-variable;?>
Make sure it is embedded javascript and not external. (Between <script></script> tags in the .php file)
It is hard to tell, what you are asking for. But assuming you want to display some PHP code within some element and you want to do this from JavaScript, this is some solution for you:
Download PHP.js script and place it on your site, then include it so it is available from other JavaScript code.
Second, use htmlentities() function on the content you want to show (the string containing PHP code). This may look like the following:
var phpCode = '<?php echo \'something goes here\'; ?>';
document.getElementById('fade').innerHTML = htmlentities(phpCode);
As a proof that it works, see this jsfiddle.

Categories