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.
$window
would not be aHTML Collect
?– Pedro Pinto
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.
– Woss
Take a look at the following link https://answall.com/questions/25136/igualar-vari%C3%A1vel-php-a-Vari%C3%A1vel-javascript
– user59482
"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.
– Woss
@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.
– Felipe Duarte
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.
– Woss
http://www.mauricioprogramador.com.br/posts/passar-variavel-javascript-para-php an example...
– Felipe Duarte