简体   繁体   English

从 JavaScript 到 PHP 的数据传输

[英]Data transfer from JavaScript to PHP

How can I get the browser's height and width to PHP?如何将浏览器的高度和宽度设置为 PHP? Like a data transfer from JavaScript to PHP?像从 JavaScript 到 PHP 的数据传输? With using innerHeight and InnerWidth , I think.我认为使用innerHeightInnerWidth

(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?我究竟做错了什么?

You would have to make an AJAX call from JavaScript to a PHP script.您必须从 JavaScript 对 PHP 脚本进行 AJAX 调用。 That PHP script would save the screen resolution in the current session (you'll need to use sessions for this).该 PHP 脚本将保存当前会话中的屏幕分辨率(为此您需要使用会话)。 A PHP script requested at a later point could then access the screen resolution as passed by the JavaScript snippet.稍后请求的 PHP 脚本可以访问 JavaScript 代码段传递的屏幕分辨率。

If you don't know AJAX then you can use links to send information to the server with the GET method.如果您不知道 AJAX,那么您可以使用链接通过 GET 方法将信息发送到服务器。

<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.这是一个不使用任何库的简单 ajax 对象。

http://www.pagecolumn.com/javascript/ajax_object.htm 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 中,

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

PHP runs on your web server . PHP 在您的Web 服务器上运行。
The PHP processor outputs HTML, which is transmitted to the client browser . PHP 处理器输出 HTML,该 HTML 被传输到客户端浏览器
-- at this point PHP is finished . - 至此 PHP完成 It can't do anything else .不能做任何其他事情
The browser now interprets the HTML page and executes the javascript - on the client machine .浏览器现在解释 HTML 页面并在客户端机器上执行 javascript。

The Javascript can't pass a value to the PHP because Javascript无法将值传递给 PHP,因为

  • The PHP is on the server while the javascript is on the client PHP 在服务器上,而 javascript 在客户端
  • The PHP has finished running by the time the javascript starts running到 javascript开始运行时,PHP 已完成运行

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.正如其他人所提到的,此时您可以做的最好的事情是将高度/宽度信息传递回服务器以供稍后使用,或者进行 AJAX 调用以根据大小信息动态更新页面。

You can get the screen resolution, but with JavaScript NOT PHP.您可以获得屏幕分辨率,但使用 JavaScript 而不是PHP。 PHP is server-side. PHP 是服务器端的。 However, you can have a JavaScript pass values to PHP using ajax or any other method.但是,您可以使用 ajax 或任何其他方法让 JavaScript 将值传递给 PHP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM