edwinv Posted September 13, 2015 Share Posted September 13, 2015 Hello, with the following code I am able to pass a variable from js to php. <script> var winW = 630, winH = 460; if (document.body && document.body.offsetWidth) { winW = document.body.offsetWidth; winH = document.body.offsetHeight; } if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) { winW = document.documentElement.offsetWidth; winH = document.documentElement.offsetHeight; } if (window.innerWidth && window.innerHeight) { winW = window.innerWidth; winH = window.innerHeight; }</script><?php $screen_width = "<script>document.write(winW)</script>"; $screen_height = "<script>document.write(winH)</script>";?> Problem: it is passed as a string and I need it to be passed as an integer. When I try (int) or intval() in php, it is set to variable type int, but it loses it value. Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/298153-javascript-variable-to-php/ Share on other sites More sharing options...
mac_gyver Posted September 13, 2015 Share Posted September 13, 2015 (edited) with the following code I am able to pass a variable from js to php no, it's not passing anything from js to php. the two php variables contain those literal strings - <script>document.write(winW)</script> and <script>document.write(winH)</script>. when you are echoing those php variables in the page that gets output to the browser, at that point there will be the - <script>document.write(winW)</script> and <script>document.write(winH)</script> code. when this code is executed in the browser they do display what you expect, because the browser is where the winW and winH variables exist. to send ANYTHING from the browser to php, you must make a http request to the server and pass the values in the http request. see the following example in the php.net documentation - http://php.net/manual/en/faq.html.php#faq.html.javascript-variable Edited September 13, 2015 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/298153-javascript-variable-to-php/#findComment-1520774 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.