-3

I am trying to set the javascript variable value in the PHP variable. But it print the javascript value.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript'>
  var height = window.screen.availHeight;
  var width = window.screen.availWidth;
</script>
<script>
  document.write(width)
</script>width
<script>
  document.write(height)
</script>

My code:-

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type='text/javascript'>
  var height = window.screen.availHeight;
  var width = window.screen.availWidth;
</script>
<?php $height = "<script>document.write(height)</script>"?>
<?php $width = '<script>document.write(width)</script>'?>
<?php echo $width.'width'; echo $height.'height'; echo'<br>'; if($height > '900'){ echo 'Good'; } else { echo 'Bad'; } ?>

It print the php variable value is width<script>document.write

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 2
    Please take the time to format the code in your questions properly. This was unreadable before I edited it. Even after editing it still doesn't make much sense. Could you please describe the problem more clearly. It seems as though you're confusing your JS and PHP logic. Note that you don't need all those ` – Rory McCrossan Oct 04 '18 at 10:37
  • 2
    `run javascript variable value inside php variable` - that makes no sense, since PHP is server side code and JS is client side – Jaromanda X Oct 04 '18 at 10:37
  • Also, Stack Snippets are only for **runnable** examples. Your examples aren't runnable, so just use code blocks. – T.J. Crowder Oct 04 '18 at 10:39
  • 1
    @RoryMcCrossan - Good point on the first. – T.J. Crowder Oct 04 '18 at 10:40
  • Send width and height from the client (javascript) to the server (php) with a form. – isalgueiro Oct 04 '18 at 10:40
  • 3
    Possible duplicate of [How to pass JavaScript variables to PHP?](https://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) – ponury-kostek Oct 04 '18 at 10:45

2 Answers2

0

You cannot pass variable values from the current page javascript to the current page PHP code... PHP code runs at the server side and it doesn't know anything about what is going on on the client side.

Here is the full explanation: https://stackoverflow.com/a/1917626/7182677

0

You can't pass it to PHP, however you can manipulate the DOM with js to achieve your goals. Just move your if statement to your js file. Add an element in your dom for example

<span id="message"></span>

And then in js

var height = window.screen.availHeight;
var width = window.screen.availWidth;
if($height > '900'){
   $('#message').innerHTML = "Good!";  
} 
else {
   $('#message').innerHTML = "Bad!"; 
}