Jump to content
New Reality: Ads For Members ×

Javascript variable to php


edwinv

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/298153-javascript-variable-to-php/
Share on other sites

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

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.