使用Javascript设置隐藏字段值;使用PHP post方法发送数据


Set hidden field value using Javascript; use PHP post method to send the data

我需要获得用户的屏幕分辨率并将其保存到我的数据库中。

我正在做的是:我有一个名为的Javascript函数,它获取信息并将值传递给一个隐藏的表单字段。这就是功能:

<script type="text/javascript">
function getUserData(){
var s_width=screen.width
var s_height=screen.height
document.getElementById('screen_width').value=s_width
document.getElementById('screen_height').value=s_height
}
</script>

形式:

<form name="login" id="login" action="memberlogin.php" method="post">
<input type="hidden" id="screen_width" name="screen_width">
<input type="hidden" id="screen_height" name="screen_height">
<table style="margin-top:9px;">
<tr><td width="200px"><input type="text" name="username" style="width:100%"/></td></tr>
<tr><td width="200px"><input type="password" name="password" style="width:100%"/></td></tr>
<tr><td><input type="submit" name="submit" value="Log In" /></td></tr>
</table>
</form>

表单提交到同一页。当我单击"提交"并尝试回显$_POST['screen_widh']和$_POST['screen_height']的值时,什么都不会发生。但当我重新提交时,价值观就在那里。有人能告诉我我做错了什么吗?如果您有其他解决方案,我们将不胜感激。

你检查过firebug的输出吗?也许有一些错误?试着在你的函数中提醒某个东西,也许它在初始页面加载后没有被调用?尝试将函数调用放入提交按钮的onClick或表单的onSubmit事件中。

我自己解决了这个问题,只是在加载元素后调用函数的问题。