TRAVA
TRAVA

Reputation: 211

Data transfer from JavaScript to PHP

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?

Upvotes: 2

Views: 2503

Answers (5)

unigg
unigg

Reputation: 464

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"; 
?>

Upvotes: 2

Bakhtiyor
Bakhtiyor

Reputation: 7318

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.

Upvotes: 2

Stephen P
Stephen P

Reputation: 14800

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.

Upvotes: 1

Pekka
Pekka

Reputation: 449385

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.

Upvotes: 4

Babiker
Babiker

Reputation: 18798

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.

Upvotes: 0

Related Questions