Jump to content

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

Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.