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; ?>');
Related
This question already has answers here:
Insert php variable in a href
(4 answers)
Closed 7 years ago.
var= www.google.com
Now I want to pass this variable to an a href tag, how shall I do it?
I tried doing below but it didn't work. I need to pass it as variable since every time the url is going to be different.
NoAbTerm
Appreciate your any help.
If you want the variable as php, you could do this:
$whatevervariable = "http://www.google.com";
/* then, you can echo it out */
echo"
<a href='".$whatevervariable."'>google</a>
";
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>
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.
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>
This question already has answers here:
How do I pass variables and data from PHP to JavaScript?
(19 answers)
Closed 8 years ago.
I'm trying to pass multiple values from PHP into my javascript function. The function is simple right now, just trying to show a popup with the values:
<script type="text/javascript">
function showMapsInfo(name, ctr) {
alert('Info = '+name +' '+ctr);
}//function showMapsInfo
</script>
When I just pass in one value, name or ctr, it works fine. However when passing in two or more values no alert occurs. Is there a reason this isn't working? I'm guessing if this is impossible I'll have to use AJAX to get the job done?
Here is the relevant PHP code. I am making multiple forms on the page, every id is unique via a ctr. I read in the $maps_name from a database. This I can output to the screen fine, so no issue there.
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo('.$maps_name.', '.$ctr.');"><img src="img/maps_logo.gif"></button><br/>');
I'm guessing you just need to quote $maps_name and $ctr beforehand (since they're most likely strings:
echo('<button type="button" id="button'.$ctr.'" onClick="showMapsInfo(\''.str_replace("'", "\\'", $maps_name).'\', \''.str_replace("'", "\\'", $ctr).'\');"><img src="img/maps_logo.gif"></button><br/>');