Pass whole javascript variable to php

Asked

Viewed 3,283 times

0

I would like to know why the following section 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 such as intval and (int) in the PHP and parseInt in the javascript it does not do the correct check, when trying to turn into integer it returns 0, which explanation?

  • $window would not be a HTML Collect?

  • 1

    Because it doesn’t make sense what you did. Research about the differences of server-side and client-side to understand when PHP and Javascript are executed. Their only way to communicate is through HTTP requests.

  • Take a look at the following link https://answall.com/questions/25136/igualar-vari%C3%A1vel-php-a-Vari%C3%A1vel-javascript

  • "this method is totally possible", no, it’s not. This way, your PHP variable will always be a string with JS code. It will only generate an entire value when this code is parsed by the browser, but PHP will no longer be present.

  • @Andersoncarloswoss , the php variable gets the correct value, ie in the example it gets "1366" which is the resolution of my screen in pixels, not the code, but it always stores as string, and the same can not be treated as integer, I would like to know why this, If you want to test the code you’ll see what I’m talking about.

  • If it receives the correct value, there must be more code than you have presented. The way it is, the JS code will not be executed by PHP.

  • http://www.mauricioprogramador.com.br/posts/passar-variavel-javascript-para-php an example...

Show 2 more comments

2 answers

3


Communication between Javascript and PHP

I tried in the comments, but I saw that it would extend too much and I decided to answer. The code you made does not work because doesn’t make sense. To only way communication between Javascript, which runs on the client side, with PHP, which runs on the server side, are HTTP messages, both the request and the reply. 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. The content is Javascript code, but it makes no difference to PHP; he will treat you like a string any and this JS code will not be parsed, because who would do this is the browser, on the client side, but we’re still running on the server side. When you make the comparison, you’re comparing a string with a int. This is no mistake because it is PHP and it allows you to do nonsense. You commented that you tried to convert to int and always gets zero; this is explained in documentation of PHP:

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

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

The value is obtained from the string’s initial portion. If the string starts with valid numerical data, this shall be the value used. Otherwise, the value will be 0 (zero). Valid numerical data is an optional sign, 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.

As his string is not a valid numeric value, PHP will always return 0. The only way, stick to the only, is to send this value via HTTP request, which can be 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, check handling the asynchronous request made by Javascript above:

<?php

$width = $_POST["width"];

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

Why did you show up string(50) "1366"?

I believe it was here that created all the confusion, because in executing the var_dump the variable contained a numerical value and not a string. This occurred because the function output var_dump 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, as the content of string is a Javascript code, the browser will understand that it should be analyzed, because it is among the tags script. That is, when displaying the value on the screen, a numerical value will be rendered referring to the value of window.innerWidth, but this does not mean that this value exists in PHP as it does not exist. It is only generated when the code is rendered by the browser. Proof of this, just access the source code of the result and you will see that in fact what arrived at the browser was the string original and not numerical value.


But in this link it says that you do so...

You quoted a link on which it apparently relied to make its code. What the site presents has nothing to do with passing the value of a Javascript variable to PHP. What he did was just generate JS code through PHP, but there is no relationship between them. For example, considering the code shown 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 parsing 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 the same, 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 has no command of the protocol used in web pages and seems to be a bad reference to follow.

  • I appreciate the answer, I have a great domain in server and client side, but this was a doubt that came to me in tests without compromise, and in this test in specific PHP really got the value

  • 1

    @Felipeduarte at the time he said about the lack of domain was referring to the author of the site you mentioned, because the material is of poor quality. I hope that the answer has been made clear and filled your doubts. If not, feel free to question what is not yet clear.

  • 1

    @Felipeduarte, yes, I think I now understand what happened. I will complete the answer with the explanation. In fact, it turns out to be something conducive to confusion.

  • 1

    @Felipeduarte edited the answer, adding the explanation of string(50) "1366". See if it’s clear enough.

  • Now it was totally clear, I did not imagine that the exit was processed in this way, it causes a lot of confusion, I thank the reply.

0

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

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

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

  • http://www.mauricioprogramador.com.br/posts/passar-variavel-javascript-para-php

  • The @Anderson has cleared up your doubt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.