将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)