我在表单中有一个隐藏字段,我试图在其中获取用户的屏幕分辨率。然后在处理端通过php检索屏幕分辨率。不幸的是,我对javascript的理解非常有限。这是我所拥有的。我真的很感激任何帮助。
<head>
<script type="text/javascript">
function xy(){
document.write(screen.width + "x" + screen.height);
document.getElementById('xy').value;
}
</script>
</head>
<form action="" method=post >
//other fields here
<input type="hidden" name="xy" id="xy" value=""/>
<input type=submit name="button" value="button" />
</form>
当我查看页面的源代码时,我不应该看到设置为例如"1366x768"的值吗?现在在处理方面,我想像这样用 php 提取信息。
if(isset($_POST['xy']) && (!empty($_POST['xy'])){
$blah = $_POST['xy'];
//sanatize $blah;
}
您可以使用
<script type="text/javascript">
function setResolution()
{
document.getElementById('xy').value = screen.width + "x" + screen.height;
}
</script>
然后,在提交时,确保执行该函数
<input type="submit" name="button" value="button" onclick="setResolution()" />
使用
function xy(){
document.getElementById('xy').value = screen.width + "x" + screen.height;
}
并使用脚本标记或在呈现表单后执行函数,否则将找不到该元素
编辑:添加小提琴
ulliw,
你不调用该函数,所以你什么也得不到...
如果你改成这个,我相信它会对你有用:
<head>
<script type="text/javascript">
document.write(screen.width + "x" + screen.height); //this will write on the html page
document.getElementById('xy').value=screen.width + "x" + screen.height; // this will put the resolution in your form's hidden field
</script>