如何将javascript变量给定的值传递给PHP变量?
我希望像如何将javascript变量给定的值传递给PHP变量?,php,ajax,Php,Ajax,我希望像屏幕宽度屏幕高度颜色深度和像素深度这样的变量可以在PHP函数中使用。基本上,我想做的是当用户进入我的网页时,例如:localhost/try.php我想知道客户机的所有上述细节,而无需设置cookie或会话属性或获取参数。是的,当然不用重新加载页面。我知道php但不知道AJAX,所以有人能帮我吗?如果我理解正确,您的问题是当用户来到您的页面(例如try.php)时,如何将javascript变量(例如screen.width screen.height)传递到php部分,以便将其作为ph
屏幕宽度
屏幕高度
颜色深度
和像素深度
这样的变量可以在PHP函数中使用。基本上,我想做的是当用户进入我的网页时,例如:localhost/try.php
我想知道客户机的所有上述细节,而无需设置cookie或会话属性或获取参数。是的,当然不用重新加载页面。我知道php
但不知道AJAX,所以有人能帮我吗?如果我理解正确,您的问题是当用户来到您的页面(例如try.php)时,如何将javascript变量(例如screen.width screen.height)传递到php部分,以便将其作为php变量
这里有一个有效的解决方案
try.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="POST" name="myForm" id="myForm">
<input type="hidden" id="screenWidth" name="screenWidth">
<input type="hidden" id="screenHeight" name="screenHeight">
</form>
<script type="text/javascript">
document.getElementById("screenWidth").value = screen.width;
document.getElementById("screenHeight").value = screen.height;
function autoSubmit ()
{
let form = document.getElementById("myForm");
form.submit();
}
window.onload = autoSubmit;
</script>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"] === 'POST')
{
if(!empty($_POST["screenWidth"]))
{
$screenWidth = $_POST["screenWidth"];
echo $screenWidth; // see if it gets the screen.width
}
if(!empty($_POST["screenHeight"]))
{
$screenHeight = $_POST["screenHeight"];
echo $screenHeight; // see if it gets the screen.height
}
}
?>
document.getElementById(“screenWidth”).value=screen.width;
document.getElementById(“屏幕高度”).value=screen.height;
函数自动提交()
{
让form=document.getElementById(“myForm”);
表单提交();
}
window.onload=自动提交;
它基本上操作DOM,将输入值的值更改为screen.width和screen.height,然后在窗口处于onload状态时自动提交表单
如果服务器收到HTTP POST方法输入,那么它会检查POST输入的名称并将其保存到每个PHP变量中。如果我理解正确,您的问题是当用户来到您的页面(例如try.PHP)时,如何传递javascript变量(例如screen.width screen.height)到PHP节,以便可以将其作为PHP变量 这里有一个有效的解决方案 try.php
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="" method="POST" name="myForm" id="myForm">
<input type="hidden" id="screenWidth" name="screenWidth">
<input type="hidden" id="screenHeight" name="screenHeight">
</form>
<script type="text/javascript">
document.getElementById("screenWidth").value = screen.width;
document.getElementById("screenHeight").value = screen.height;
function autoSubmit ()
{
let form = document.getElementById("myForm");
form.submit();
}
window.onload = autoSubmit;
</script>
</body>
</html>
<?php
if($_SERVER["REQUEST_METHOD"] === 'POST')
{
if(!empty($_POST["screenWidth"]))
{
$screenWidth = $_POST["screenWidth"];
echo $screenWidth; // see if it gets the screen.width
}
if(!empty($_POST["screenHeight"]))
{
$screenHeight = $_POST["screenHeight"];
echo $screenHeight; // see if it gets the screen.height
}
}
?>
document.getElementById(“screenWidth”).value=screen.width;
document.getElementById(“屏幕高度”).value=screen.height;
函数自动提交()
{
让form=document.getElementById(“myForm”);
表单提交();
}
window.onload=自动提交;
它基本上操作DOM,将输入值的值更改为screen.width和screen.height,然后在窗口处于onload状态时自动提交表单
如果服务器收到HTTP POST方法输入,那么它会检查POST输入的名称并将其保存到每个PHP变量中。请阅读。如果您需要这里的帮助,那么您需要问一个特定的问题,通常是关于您已经编写的代码的问题。对于“我不知道Ajax”,这里唯一可以接受的回答应该是,我们开始阅读它的基础知识。这个网站不是一个教授基础知识的地方,你可以在其他地方轻松地了解自己。请去阅读。如果您需要这里的帮助,那么您需要问一个特定的问题,通常是关于您已经编写的代码的问题。对于“我不知道Ajax”,这里唯一可以接受的回答应该是,我们开始阅读它的基础知识。这个网站不是一个绝对基础知识的教学场所,你可以在其他地方轻松阅读。