将javascript值分配给php变量 [英] Assign javascript value to php variable

查看:146
本文介绍了将javascript值分配给php变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查屏幕宽度并将其分配给php变量以执行一些if else语句。这就是我得到的。

I'm trying to check screen width and assign it to a php variable to do some if else statements. This is what I got.

<script>
    var mobileFormWidthCheck = $(window).width();
    var mobileFormReady;
    if(mobileFormWidthCheck < 767){
        mobileFormReady = 22;
    }
    else{
        mobileFormReady = 55;  
    }

</script>

    <?php   
        $widthChecked = "<script>document.write(mobileFormReady);</script>";
        echo $widthChecked;
    ?>

这完美无缺。但是,当我尝试基于 mobileFormReady 值回显某些内容时,它不会回显。

This works perfectly. But when I try to echo something based on the mobileFormReady value, it doesnt echo.

这就是我试图开始工作的原因。

This is what im trying to get to work.

$widthChecked = "<script>document.write(mobileFormReady);</script>";
if($widthChecked == "22"){
  echo 'this page is under 767 pixels';
}
else if($widthChecked == "55"){
  echo 'this page is OVER 767 pixels';
}
else{
  echo 'NOT WORKING YET';
}

我认为这是一个字符串整数问题。但我似乎无法弄明白。你能帮我吗?非常感谢。

I think its a string integer issue. But I cant seem to figure it out. Can you guys please help me? Thanks a lot.

推荐答案

我想你可以使用cookies。

I guess you can use cookies for it.

1)首先添加一个cookie jquery插件。

1) First add a cookie jquery plugin.

2)然后将该窗口宽度存储在cookie变量中。

2) Then store that window width in a cookie variable.

3)使用PHP访问您的cookie,如$ _COOKIE ['变量名称']。( http ://www.w3schools.com/php/php_cookies.asp

3) Access your cookie in PHP like $_COOKIE['variable name'].(http://www.w3schools.com/php/php_cookies.asp)

这篇关于将javascript值分配给php变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆