2

How can I get the browser's height and width to PHP? Like a data transfer from JavaScript to PHP? With using innerHeight and InnerWidth, I think.

(I just need to show user small picture if he has small screensize and big if big and without a data about screensize I can't do it)

I have like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<link rel="stylesheet" href="css/slideshow.css" type="text/css" /> 

<script> 
  document.write('script.php?screen=' + screen.width + 'x' + screen.height'); 
</script> 

</head> 
<body> 

  <?php $lol=$_GET['screen']; 
  <?php echo "SIZE : $lol" ?> 

</body> 
</html> 

And it doesn't work. What am I doing wrong?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
TRAVA
  • 211
  • 2
  • 12

5 Answers5

4

You would have to make an AJAX call from JavaScript to a PHP script. That PHP script would save the screen resolution in the current session (you'll need to use sessions for this). A PHP script requested at a later point could then access the screen resolution as passed by the JavaScript snippet.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • I'm so bad in AJAX. It could be done just with javascript and php. When they are on the same page. – TRAVA Jun 18 '10 at 20:42
  • 2
    @Trava no, it can't be done without Ajax, sorry. Javascript runs after PHP has generated the page, and has no chance of passing anything back to PHP. This is why Ajax exists. – Pekka Jun 18 '10 at 20:43
  • @Trava check out jQuery's Ajax examples: http://api.jquery.com/jQuery.ajax/ it's easy to get into, just a few lines of code. – Pekka Jun 18 '10 at 20:47
2

If you don't know AJAX then you can use links to send information to the server with the GET method.

<script language="javascript">
   function load(){
      document.getElementById('myAnchor').href="test2.php?sw="+screen.width+"&sh="+screen.height;
   }
</script>
<body onload="load();">
<a id="myAnchor" href="#">Send to server</a>
<?php
if (isset($_GET)){
    print_r($_GET);
}
?> 
</body>

Or you can also use forms to send information to the server.

Bakhtiyor
  • 7,198
  • 15
  • 55
  • 77
2

Here is a smple ajax object without using any library.

http://www.pagecolumn.com/javascript/ajax_object.htm

In client side, use it like,

function foo() {
    var screen=' + screen.width + 'x' + screen.height'; 
    ajax.getRequest("test_get.php",["screen"], [screen],function(data){
       alert(data);
    });
}

In PHP,

<?php $lol=$_GET['screen']; 
 echo "SIZE : $lol"; 
?>
unigg
  • 466
  • 3
  • 8
1

PHP runs on your web server.
The PHP processor outputs HTML, which is transmitted to the client browser.
-- at this point PHP is finished. It can't do anything else.
The browser now interprets the HTML page and executes the javascript - on the client machine.

The Javascript can't pass a value to the PHP because

  • The PHP is on the server while the javascript is on the client
  • The PHP has finished running by the time the javascript starts running

As other people have mentioned, the best you can do at this point is pass the height/width information back to the server to be used later or make an AJAX call to update the page dynamically given the size information.

Stephen P
  • 14,422
  • 2
  • 43
  • 67
0

You can get the screen resolution, but with JavaScript NOT PHP. PHP is server-side. However, you can have a JavaScript pass values to PHP using ajax or any other method.

Babiker
  • 18,300
  • 28
  • 78
  • 125