how to pass js integer value as integer in PHP? [duplicate] - javascript

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 7 years ago.
<script type="text/javascript">
var int_val = 111;
</script>
<?php
$js_int_val_in_php ='<script>document.write(int_val);</script>';
echo $js_int_val_in_php; // 111
echo '<br>';
echo gettype($js_int_val_in_php); // string
// but I want it as an integer
// even settype(); does not help!!
?>
Anyone has any good idea how do I pass js integer value as integer in PHP?? I love jQuery but in this situation, please no jQuery suggestion.

You are confusing server-side variables with client-side variables,
You need to the data to the server, otherwise the server only knows the variable name, which is a string
<script>
var int_val=111;
function doAjax()
{
var xmlhttp;
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET","index.php?v="+int_val,true);
xmlhttp.send();
}
</script>
click here to send data to server
<?php
$int_val=''
if (isset($_GET['v']))
{$int_val=intval($_GET['v']);}
//now you can access the `int_val` after the JS function ran
?>

I think that the question was already asked here: Unable to convert string to integer in PHP
If you want, you can also look at PHP typecasting here: http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
and maybe have something like this?
<script type="text/javascript"> var int_val = 111</script>
<?php
$js_int_val_in_php = '<script>document.write(int_val);</script>';
echo $js_int_val_in_php; // 111
echo '<br>';
$toInt = (int) $js_int_val_in_php;
echo '<br>';
echo gettype($toInt); // string
?>
I haven't tested but I'm sure the typecasting should do the trick, don't forget to have a look at the URL I sent first

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.

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.

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