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

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

Related

Getting the incorrect result while initializing the php variable to java script variable [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Warning: mysqli_query() expects parameter 1 to be mysqli, string given in
(4 answers)
Closed 1 year ago.
I have a php page home.php and in this I have a php variable $disableCreate = 'true'; . Please see the below code.
<?php
ob_start();
include_once("app_header.php");
$disableVar = 'true';
//below code to show the home page template
$htmlrenderObj = Htmlrender::getObj();
$template_path = $htmlrenderObj->app_home_template_path."app_home.html";
$template = $htmlrenderObj->return_file_content($template_path);
ob_end_flush();
include_once("app_footer.php");
?>
Now in the java script section I'm trying to access that variable and assign it to java script variable.
<script>
var xmlHttp = getAjaxObj();
var myJsVar = "<?php echo $disableVar; ?>";
var myJsVar2 = "<?php echo json_encode($disableVar); ?>";
var myJsVar3 = "<?=$disableVar?>";
var myJsVar4 = "$disableVar";
console.log("$$$$$$$$ myJsVar is:"+myJsVar);
console.log("$$$$$$$$ myJsVar is:"+myJsVar2);
console.log("$$$$$$$$ myJsVar is:"+myJsVar3);
console.log("$$$$$$$$ myJsVar is:"+myJsVar4);
</script>
I tried different ways to initialize my java script variable with php variable. But not get the desired output. Please see the below screenshot for reference.
Please help me.

how to execute javascript code inside php? [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 2 years ago.
php file
<button id='submit' name='submit' ><h1>Schedule</h1></button>
<?php
include 'dbres.php';
if (isset($_POST['submit'])){
$sql = "UPDATE reserved SET reserved = false WHERE reserved = true;";
$res = mysqli_query($conn,$sql);
echo'document.getElementById("submit").onclick = function() {
this.textContent = "reserved";
}';
}
?>
I want the javascript code inside the php code to changes the text from submit to reserved. I have tested the javascript code and it works by itself, but doesn't seem to work in php.
You cannot echo JS inside PHP, they are two separate languages. If you want to have the client run the JS, you can use the script tag:
echo '<script>
document.getElementById("submit").onclick = function() {
this.textContent = "reserved";
}
</script>';

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

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.

Struggling to use PHP inside JavaScript [duplicate]

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

Categories

Resources