使用本机ajax将Javascript函数输出分配给PHP会话变量[无需JQuery和用户操作(无点击)]
我的js知识几乎为零 我想使用本机ajax将Javascript函数输出分配给PHP会话变量[无需JQuery和用户操作(无点击)],javascript,php,ajax,variables,parameter-passing,Javascript,Php,Ajax,Variables,Parameter Passing,我的js知识几乎为零 我想 使用js在my index.php中获取屏幕宽度(没问题) 使用ajax将js输出值分配给php会话变量(?!?) 将这些值添加到数据库以用于未来目标(没问题) 而且 我不想使用jquery库。(我唯一的js用法就是在这个时候 (点) 我不想让任何按钮点击。(无访客活动) 我试过的如下。我创建了两个页面:index.php和dealviewportwidth.php index.php将返回viewport width,也将完成ajax部分,我将通过$\u GET
- 我不想使用jquery库。(我唯一的js用法就是在这个时候 (点)
- 我不想让任何按钮点击。(无访客活动)
index.php
和dealviewportwidth.php
index.php将返回viewport width,也将完成ajax部分,我将通过$\u GET
在dealviewportwidth.php中赋值
如果失败,则未设置会话变量。index.php中的//assignWidthToPHP部分中要更正什么,以便设置会话变量
index.php(通过js返回宽度,处理ajax)
<script type="text/javascript">
function getViewportWidth()
{
var viewportwidth;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth
}
return(viewportwidth);
}
// assignWidthToPHP
var xhttp = new XMLHttpRequest();
var width = getViewportWidth();
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
xhttp.send();
</script>
函数getViewportWidth()
{
变量视口宽度;
//更符合标准的浏览器(mozilla/netscape/opera/IE7)使用window.innerWidth和window.innerHeight
if(typeof window.innerWidth!=“未定义”)
{
viewportwidth=window.innerWidth
}
//IE6处于标准兼容模式(即,文档中的第一行是有效的doctype)
else if(typeof document.documentElement!=“未定义”
&&typeof document.documentElement.clientWidth=
“未定义”&&document.documentElement.clientWidth!=0)
{
viewportwidth=document.documentElement.clientWidth
}
//IE的旧版本
其他的
{
viewportwidth=document.getElementsByTagName('body')[0]。clientWidth
}
返回(视口宽度);
}
//分配宽度TopHP
var xhttp=newXMLHttpRequest();
var width=getViewportWidth();
xhttp.open(“GET”http://localhost/dealviewportwidth.php?w=“+宽度,错误);
xhttp.send();
dealviewportwidth.php
<script type="text/javascript">
function getViewportWidth()
{
var viewportwidth;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth
}
return(viewportwidth);
}
// assignWidthToPHP
var xhttp = new XMLHttpRequest();
var width = getViewportWidth();
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
xhttp.send();
</script>
<?php
session_start ();
$_SESSION["w"] = $_GET["w"];
var_dump($_SESSION["w"]);
?>
<?php
session_start();
echo $_SESSION["w"];
您的脚本正在我的计算机上运行,我刚刚更改了这一行:
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
到
写第三个脚本:
check.php
<script type="text/javascript">
function getViewportWidth()
{
var viewportwidth;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth
}
return(viewportwidth);
}
// assignWidthToPHP
var xhttp = new XMLHttpRequest();
var width = getViewportWidth();
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
xhttp.send();
</script>
<?php
session_start ();
$_SESSION["w"] = $_GET["w"];
var_dump($_SESSION["w"]);
?>
<?php
session_start();
echo $_SESSION["w"];
您的脚本正在我的计算机上运行,我刚刚更改了这一行:
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
到
写第三个脚本:
check.php
<script type="text/javascript">
function getViewportWidth()
{
var viewportwidth;
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
if (typeof window.innerWidth != 'undefined')
{
viewportwidth = window.innerWidth
}
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
else if (typeof document.documentElement != 'undefined'
&& typeof document.documentElement.clientWidth !=
'undefined' && document.documentElement.clientWidth != 0)
{
viewportwidth = document.documentElement.clientWidth
}
// older versions of IE
else
{
viewportwidth = document.getElementsByTagName('body')[0].clientWidth
}
return(viewportwidth);
}
// assignWidthToPHP
var xhttp = new XMLHttpRequest();
var width = getViewportWidth();
xhttp.open("GET", "http://localhost/dealviewportwidth.php?w="+width,false);
xhttp.send();
</script>
<?php
session_start ();
$_SESSION["w"] = $_GET["w"];
var_dump($_SESSION["w"]);
?>
<?php
session_start();
echo $_SESSION["w"];
您首先运行index.php,然后运行另一个并运行了吗?为什么我不能呢?它可能依赖于浏览器吗?dealviewportwidth.php由index.php在后台调用,您可以在浏览器的网络检查器中看到该调用。你用什么浏览器?我编辑了我的答案,我认为用第三个脚本显示会话变量应该更容易。好的,我知道了。我也从ie的网络检查员那里查过了,它就在那里。那么为什么我不能通过直接调用dealviewportwidth来单击dealviewportwidth.php
来设置会话变量呢?您没有传递“w”参数,所以$\u GET[“w”]为空,您可以清除会话变量。。。尝试直接访问“dealviewportwidth.php?w=foo”,它应该先运行index.php,然后运行另一个,然后工作?为什么我不能呢?它可能依赖于浏览器吗?dealviewportwidth.php由index.php在后台调用,您可以在浏览器的网络检查器中看到该调用。你用什么浏览器?我编辑了我的答案,我认为用第三个脚本显示会话变量应该更容易。好的,我知道了。我也从ie的网络检查员那里查过了,它就在那里。那么为什么我不能通过直接调用dealviewportwidth来单击dealviewportwidth.php
来设置会话变量呢?您没有传递“w”参数,所以$\u GET[“w”]为空,您可以清除会话变量。。。尝试直接访问“dealviewportwidth.php?w=foo”,它应该可以完成这项工作