admin管理员组文章数量:1326328
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 badges5 Answers
Reset to default 4You 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.
本文标签: Data transfer from JavaScript to PHPStack Overflow
版权声明:本文标题:Data transfer from JavaScript to PHP - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742203155a2432330.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论