Pass entire variable from javascript to php [duplicate]

0

I would like to know why the following snippet does not work ...

<?php
$window = '<script>document.write(window.innerWidth)</script>';

//exemplo de saída 'string(50) "1366"'

if($window > 920) {
    echo 'maior';
}else{
    echo 'menor';
}
?>

Even using methods like intval and (int) in PHP and parseInt in javascript it does not make the correct scan, when trying to transform to integer it returns 0, what's the explanation?

    
asked by anonymous 04.09.2017 / 06:40

2 answers

4

Communication between JavaScript and PHP

I tried the comments, but I saw that it would go too far and I decided to respond. The code you did not work because does not make sense . The single way of communication between JavaScript, which runs on the client side, with PHP, which runs on the server side, is HTTP messages , both the request and the response . Considering the code presented:

<?php
$window = '<script>document.write(window.innerWidth)</script>';

//exemplo de saída 'string(50) "1366"'

if($window > 920) {
    echo 'maior';
}else{
    echo 'menor';
}
?>

First, you assign a string to the variable $window . Content is JavaScript code, but that does not make a difference to PHP; it will treat it as any string and this JS code will not be parsed , because who would do this is the browser on the client side but we are still running on the side the server. When you do the comparison, you are comparing a string with an int . This gives no error because it is PHP and it allows you to do nonsense things. You commented that you tried to convert to int and always get zero; this is explained in the PHP documentation :

  

When a string is interpreted as a numeric value, the value   and its type is determined as follows.

     

If the string does not contain any of the characters '.', 'e', or 'E' and the   numerical value fits within the limits of the integer type (defined   PHP_INT_MAX constant), the string evaluates to an integer.   In all other cases it will be interpreted as a float.

     

The value is derived from the initial portion of the string. If the string starts with   valid numeric data, this is the value used. Otherwise,   the value will be 0 (zero) . Valid numeric data is an optional signal,   followed by one or more digits (optionally containing a dot   decimal), followed by an exponent, also optional. The exponent is an 'e'   or 'E' followed by one or more digits.

Since your string is not a valid numeric value, PHP will always return 0 . The only way to be aware of the unique is to send this value via HTTP request, which can be either synchronous or asynchronous, depending on what you want to do.

With jQuery, for example, you could do something like:

$.post("window.php", {width: window.innerWidth}).done(data => {
    alert(data);
});

And in PHP, do the check by handling the asynchronous request made by JavaScript above:

<?php

$width = $_POST["width"];

if ($width > 920) {
     echo "maior";
} else {
     echo "menor";
}

Why did string(50) "1366" appear?

I believe it was all the confusion here, because when running the var_dump of the variable, it consisted of a numeric value and not the string . This occurred because the output of the var_dump function was sent to the browser via HTTP response and the output of this function is:

string(50) "<script>document.write(window.innerWidth)</script>"

The browser received this value and reproduced it on the screen. However, since the string content is a JavaScript code, the browser will understand that it should be parsed, since it is between the tags tag script . That is, when displaying the value on the screen, a numeric value will be rendered referring to the value of window.innerWidth , but that does not mean that this value exists in PHP, since it does not exist. It is only generated when the code is rendered by the browser. Proof of this, you just have to access the source code of the result and you will see that what actually came to the browser was the original string and not the numeric value.

But this link says that if it does so ...

You cited a link that apparently was based on making your code . What the site presents has nothing to do with passing the value of a JavaScript variable to PHP. What it did was just generate JS code through PHP , but there is no relationship between them. For example, considering the code displayed on the page:

<html>
<head>
 <title>Passar Variável Javascript para PHP</title>
 <script type="text/javascript">
  var variaveljs = 'Mauricio Programador'; 
 </script>
</head>
<body>
 <?php 
  $variavelphp = "<script>document.write(variaveljs)</script>";
  echo "Olá $variavelphp";
 ?>
</body>
</html>

What is done is that PHP will handle the request for this file and, when analyzing PHP, will generate the following result:

<html>
<head>
 <title>Passar Variável Javascript para PHP</title>
 <script type="text/javascript">
  var variaveljs = 'Mauricio Programador'; 
 </script>
</head>
<body>
  Olá <script>document.write(variaveljs)</script>
</body>
</html>

This content will be sent to the browser via HTTP response and will be analyzed by displaying the name on the screen, but at no time was the JS variable available in PHP. Everything indicates that the author of this site does not have domain of the protocol used in web pages and seems to be a terrible reference to follow.

    
04.09.2017 / 17:44
0

PHP is a server side language. That means: it runs on the server. Javascript is a client side language. That is, it runs in the browser interface.

Having said that, it is important to answer that PHP, when running on the server, has zero knowledge about how it will be used. It runs and its output can be consumed by some other source: browser (through ajax requests or direct access), command line, among others.

So, window.innerWidth means nothing to the server. It is thus not possible to perform javascript routines through PHP (at least, not that way).

    
04.09.2017 / 16:11