Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取页面宽度Javascript-返回PHP变量_Javascript_Php_Jquery - Fatal编程技术网

获取页面宽度Javascript-返回PHP变量

获取页面宽度Javascript-返回PHP变量,javascript,php,jquery,Javascript,Php,Jquery,我试图做一些相当简单的事情,但我不是一个好的javascript。我想做的是: 1.通过Javascript获取页面宽度 2.如果页面宽度小于400px,则将php变量设置为1 我的JAVASCRIPT很差 //javascript var width = $(window).width(); //get the width of the screen if (width<400) { $slidevar = 1; } 所以这是不可能的 你能在页面加载时用PHP一次性检

我试图做一些相当简单的事情,但我不是一个好的javascript。我想做的是: 1.通过Javascript获取页面宽度 2.如果页面宽度小于400px,则将php变量设置为1

我的JAVASCRIPT很差

 //javascript
 var width = $(window).width(); //get the width of the screen
 if (width<400) {
  $slidevar = 1;
   } 
所以这是不可能的

你能在页面加载时用PHP一次性检索页面宽度吗

我找到了这段代码,但是我需要怎么做才能得到我的函数呢

  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
   <script>
  var width = $(window).width();
   //This is ajax code which will send width to your php page in the   screenWidth variable
$.ajax({
url: "example.php", //this will be your php page 
data : {screenwidth:width} 
}).done(function() {
$(this).addClass("done");
});
</script>

//example.php code is here :
<?php
echo $width = $_REQUEST['screenwidth'];
?>

变量宽度=$(窗口).width();
//这是ajax代码,它将在screenWidth变量中向php页面发送宽度
$.ajax({
url:“example.php”//这将是您的php页面
数据:{screenwidth:width}
}).done(函数(){
$(此).addClass(“完成”);
});
//下面是example.php代码:

我们在这里可以做到的是

在javascript中,计算屏幕宽度并进行ajax调用以相应地渲染滑块

var width = screen.width;
if (width < 400) {
   //AJAX call to render slider1
} else {
   //AJAX call to render slider2
}
var-width=screen.width;
如果(宽度<400){
//渲染幻灯片1的AJAX调用
}否则{
//渲染幻灯片2的AJAX调用
}
jQuery:

var width = $(window).width(); //get the width of the screen
if (width<400) {
  var slidevar = 1;
  $.ajax({
    url:"the/php/page",
    data: {'slidevar':slidevar},
    type: 'get',
    success: function(){
       // do something
    }
  });

你知道客户端脚本和服务器端脚本的区别,它们在哪里运行,或者我说哪个先运行?好的,是的,不知道。是的,PHP是服务器端,java是客户端。因此,无法在PHP中获取变量。关于使用Javascript关闭和打开php函数有什么建议吗?我应该说“有什么窍门吗”?您能用PHP检索页面吗?您不能在javascript(客户端)中使用或声明PHP(服务器端)变量。使用ajax将参数从jquery发送到php或vise Versal如果您熟悉ajax,可以查看我的答案,否则我将根据您的需要更新ajax调用的答案
var width = $(window).width(); //get the width of the screen
if (width<400) {
  var slidevar = 1;
  $.ajax({
    url:"the/php/page",
    data: {'slidevar':slidevar},
    type: 'get',
    success: function(){
       // do something
    }
  });
$slidevar = $_GET['slidevar'];
if ($slidevar == 1) {
  //Display Slideshow #1 PHP Function
} else {
 //Display Slideshow #2 PHP Function
}