最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Data transfer from JavaScript to PHP - Stack Overflow

programmeradmin1浏览0评论

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" ".dtd"> 
<html xmlns=";> 
<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?

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/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3/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?

Share Improve this question edited Nov 15, 2020 at 11:34 Donald Duck is with Ukraine 8,91223 gold badges79 silver badges102 bronze badges asked Jun 18, 2010 at 20:24 TRAVATRAVA 2113 silver badges13 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 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.

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.

Here is a smple ajax object without using any library.

http://www.pagecolumn./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"; 
?>

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.

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.

发布评论

评论列表(0)

  1. 暂无评论