Struggling to use PHP inside JavaScript [duplicate] - javascript

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
In one PHP file I have a variable named $difficulty.
I need to access this file in my javascript file, game.js.
I have tried this inside my game.js file:
userDif = "<?php echo json_encode($difficulty); ?>";
console.log("Difficulty " + userDif);
However, this does not work, it just prints out "<?php echo json_encode($difficulty); ?>"
I have also tried just:
userDif = <?php echo json_encode($difficulty); ?>;
But then you get an error as it doesn't expect "<"
Any help would be greatly appreciated.
Edit: Apologies: This has already been answered! I was just searching for the wrong thing. Sorry!

Don't put quotes around the value. json_encode() will put quotes around the string, so you end up with extra quotes. It should be:
userDif = <?php echo json_encode($difficulty); ?>;

I think you have something like this:
Php file
Js file included in php file
You have to run js code with php inside the php file, then you can use that variable inside the js file.
Php file:
<script type="text/javascript">
var phpCode = "<?php echo 'hola'; ?>";
</script>
<script src="scriptFile.js" type="text/javascript"></script>
scriptFile.js:
alert(phpCode);

Related

It is necessary in php code where there is a js code to output, for example, through an alert variable [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
It is necessary in PHP code where there is a js code to output, for example, through an alert variable. Does not work. Please help, tell me how to do it.
And if anyone can how to use this as a URL to the picture
function setLocation($url_image){
$locationName = R::load('locationsdb', $url_image);
echo '<script type="text/javascript">',
'alert("'<?php echo $locationName; ?>'");',
'var img = "<?php echo $locationName ?>";',
'document.getElementById("image_location").src = img;',
'</script>';
}
alert("{"id":"3","location_name":"\u0410\u043b\u0445\u0438\u043c\u0438\u043a\r\n","location_description":"\u0423 \u044d\u0442\u043e\u0433\u043e \u0441\u0442\u0430\u0440\u0446\u0430 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0435\u0441\u0442\u0438 \u043d\u0430\u0441\u0442\u043e\u0439\u043a\u0438, \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u044b\u0435 \u043f\u0440\u0438\u0434\u0430\u0432\u0430\u0442\u044c \u0441\u0438\u043b \u0433\u0435\u0440\u043e\u044f\u043c, \u0438\u0441\u0446\u0435\u043b\u044f\u0442\u044c \u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u043e\u0436\u0435\u0441\u0442\u043e\u0447\u0435\u043d\u043d\u044b\u0445 \u0431\u043e\u0435\u0432 \u0438 \u043c\u043d\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435, \u0447\u0442\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u043c\u043e\u0447\u044c \u0432 \u0442\u044f\u0436\u043a\u043e\u0439 \u0436\u0438\u0437\u043d\u0438 \u0438\u0441\u043a\u0430\u0442\u0435\u043b\u044f \u043f\u0440\u0438\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0439.\r\n","location_travel":"2","location_lut":"-","location_mobs":"-","location_image":"../images/locationsBackgrounds/backgroundAlchemistImage.png","location_runs":"-","location_characters":"-"}");
You can't use <?php tags inside each other. It makes no sense anyway - you're already in the PHP block, you don't need to open it again. And for similar reasons you can't use echo inside another echo command. And again it makes no sense anyway, you're already echoing, so why repeat the same instruction before you've finished the first one?
To generate the text you want, just use string concatenation with the . (dot) operator:
echo '<script type="text/javascript">
alert("'.$locationName.'");
var img = "'.$locationName.'";
document.getElementById("image_location").src = img;
</script>';

PHP variables into javascript [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
PHP echo in echo
(1 answer)
Closed 2 years ago.
I'm trying to get PHP variables into javascript however I'm having some issues. In testing, I'm trying to verify that it works. I've used variations of the following code and either got an alert with the actual PHP call or got an error.
Tried this: (an alert is generated but of the actual PHP call as a string basically, not the actual naming variable or moo)
function jvtest()
{
$naming = "moo";
echo '<script type="text/javascript">
var x = "<?php echo $naming ?>";
alert(x);
</script>';
}
And this. Generates a PHP error.
function jvtest()
{
$naming = "moo";
echo '<script type="text/javascript">
var x = "'<?php echo $naming ?>'"; <-- error on this line
alert(x);
</script>';
}
Any idea of how to resolve this? Thanks

Get current session value in javascript not working [duplicate]

This question already has answers here:
PHP code is not being executed, but the code shows in the browser source code
(35 answers)
Closed 4 years ago.
At the top of the page, Before the html tag, I have this
<?php
session_start();
$str = "f=6&t=3&e=1&view=unread#unread";
$_SESSION['params'] = $str;
?>
Inside the HTML in the head section I have this:
<script>
window.onload = function()
{
var url = "http://beleuramyhome.org.au/phpBB3/viewtopic.php?";
var args = "<?php echo $_SESSION['params'] ?>";
url += args;
alert(url);
}
</script>
But what I get is this , Why?
http://beleuramyhome.org.au/phpBB3/viewtopic.php?<?php echo
$_SESSION['params'] ?>
Instead of the combined strings?
If your $_SESSION['params'] is an array then you need to implode it and then use the urlencode() PHP function to encode it
urlencode(implode('&', $_SESSION['params']))
else just use urlencode() PHP function to encode your string
urlencode($_SESSION['params'])
I you still don't get required output, first check what type of data, or if any data, $_SESSION['params'] returns.
Hope this helps.

PHP - Javascript integration? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 7 years ago.
Suppose you are in a php file and you want to run a javascript function that has both javascript and php inside of it. How would you go about doing this?
Ex:
<?php
<script>
function myFunction(){
// php code
document.write("Hello World!");
}
</script>
?>
I know I can easily make a php function for this particular code, but just for future reference, I would like to know how to do this.
I apologize if this has been answered before but it seemed difficult to word in google search.
The PHP code has to output text that is valid inside the javascript. Something like this.
<?php
<script>
function myFunction(){
// php code
var message = "<?php echo "My alert message"; ?>";
window.alert(message);
}
</script>
?>

Use PHP variable in javascript function within a Smarty template [duplicate]

This question already has answers here:
How to access PHP variables in JavaScript or jQuery rather than <?php echo $variable ?> [duplicate]
(6 answers)
Closed 9 years ago.
In my HTML file, I'm using a string variable called {$var} which is passed from a PHP file. How could I use {$var} in a javascript function within the same html file? I would like to display this variable using the js function. This is what I have so far:
<span id="printHere"></span>
<script type="text/javascript">
var php_var = {$production};
$('#printHere').html(php_var);
</script>
For PHP
You can echo out the variable directly into your JavaScript. Just be sure to json_encode() it so that data types and escaping are all done automatically.
var php_var = <?php echo json_encode($production) ?>;
For Smarty
If you are using Smarty for your templating engine, you want this instead:
var php_var = {$production|json_encode nofilter};
What this does is disable the HTML escaping of Smarty (with nofilter) and passes the value through json_encode().
Make sure $production is set. If it is a (javascript) String then use:
var php_var = '<?php echo addslashes($production); ?>';
If it is a Number then use
var php_var = <?php echo $production; ?>;

Categories

Resources