save jscript integer as a php integer [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.
I'm trying to get the browser's width and then compare it as a php variable, but it is saving it as a string, I tried parsing it to another variable but it only returns 0.
$tam='<script>var j= $(window).width(); document.write(j)</script>';
I'm doing the parsing like this:
$ntam=(int) $tam;

You can't save a javascript value into a php variable directly. But you can....Put the php value into a javascript variable and use that for making your changes using javascript.
<?php echo '<script type="text/javascript">var jVar = "$phpVar";'; ?>
If you are importing your script from a js file you will want to add this before your import or course.
And I'm not sure what you're trying to accomplish but if you need a js value you can always send that value using an ajax Request...
var ajax = new XMLHttpRequest();
var form = new FormData();
form.append('win_size',window.innerWidth);
ajax.open("http://example.com/processJSWindowSize.php","POST",true);
ajax.send(form);
On the server side code, which in this example is processJSWindowSize.php, you can have something along the lines of
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$jsWindowSize = strip_tags(stripslashes($_POST['win_size']));
//do what you need with the window size
}
?>

Your script needs to return the width and it needs to be a function
<script>
var tam= function()
{
var j= $(window).width();
document.write(j)
return $(window).width();
}
var ntam = tam;
</script>

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 retrieve setAttribute from servlet into JavaScript [duplicate]

This question already has answers here:
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
(5 answers)
Closed 6 years ago.
I'm trying to retrieve the value of my setAttribute into my javascript code but I get a syntax error when I run the page.
Here is the code of my setAttribute in my servlet:
request.setAttribute("codeIGA", codeIGA); //a string is being passed here
Here is the code of my JS:
$(document).ready(
function () {
var count = 0;
var lastCodeIGA = ${sessionScope.codeIGA}; // syntax error here
console.log(lastCodeIGA);
.
.
.
etc
How can I retrieve the value from my servlet and input it into my JS?
You can use following to retrieve the required value.
var lastCodeIGA = '${codeIGA}';
Try with scriptlet.
var test = <%=request.getAttribute("codeIGA");
Or
var lastCodeIGA = '${codeIGA}';

Pass a variable from a javascript function to another php file [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i am new to javascript and php and i need your help. I have a javascript function inside a php file. In this javascript function i have a variable that i want to pass it to another php file. Can anyone help me how to do this.
Thanks
There are several ways you can do this. Probably the simplest way is:
var myData = 'whatever';
window.location.href = 'otherscript.php?myData=' + myData;
Then your script 'otherscript.php' can access that variable with $_GET['myData'].
This method requires a page refresh. You can do it without refreshing the page by using ajax, but that is a little bit trickier.
Something like that should work:
<script type="text/javascript">
function yourFunction(yourParameter) {
<?php $abc = "<script>document.write(yourParameter)</script>"?>
}
</script>
<?php
echo $abc;
?>

Echo PHP Code in Javascript [duplicate]

This question already has answers here:
How do I pass JavaScript variables to PHP?
(16 answers)
Closed 8 years ago.
I would like to echo some php code in javascript for certain reasons but the code does not get executed, when i use the code inspector i notice that the php code has been commented out.
This is the code:
var code = '<?php echo time_passed(strtotime(' + new Date().toLocaleString() + ')); ?>';
$('#page').html(code);
basically i am passing the javascript datetime value to a php function which should then echo the results into the code variable.
I then output the value of code to a container with an id of page.
PHP is on the server end. In the manner your using, there is no way to for PHP and JavaScript code execution to interact. You would need to user AJAX to communicate between front end execution (JavaScript) and back end execution (PHP).

use Js Variable in php [duplicate]

This question already has answers here:
What is the difference between client-side and server-side programming?
(3 answers)
Closed 8 years ago.
i want to use javascript varible in php..i refered much stackoverflow link and google but i didn't get proper solution..i also know js is client side scripting language and php is serverside..but i want js varible in php and i had also try with ajax call but i want set time zone in this file not other file so ajax call is not working in my situation.any one help me how to get same result for varible in echo and var_dump...
<html>
<script type='text/javascript'>
var timezone = jstz.determine();
var timezone=timezone.name();
console.log(timezone);
// it print 'Asia/Kolkata';
<?php $abc = "<script type='text/javascript'>document.write(timezone)</script>"?>
</script>
<?php
echo $abc;
// it print 'Asia/Kolkata';
var_dump($abc);
// it print string '<script type='text/javascript'>document.write(timezone)</script>' (length=65)
?>
</html>
any help must be appreciated..Thnks
you can use php variables to javascript but not javascript variables to php.. because javascript executes on client side and php executes on server side.. so php is executed before javascript gets executed..
So simply you cant...
the only thing you can do this by using ajax....

Categories

Resources