使用Javascript设置隐藏字段值;使用PHP post方法发送数据 [英] Set hidden field value using Javascript; use PHP post method to send the data
问题描述
我需要获取用户的屏幕分辨率并将其保存在我的数据库中.
I need to get the user's screen resolution and save it my database.
我正在做的是:我有一个名为的Javascript函数,它获取信息并将值传递给隐藏的表单字段.这是功能:
What I'm doing is this: I have a Javascript function called with that gets the info and passes the value to a hidden form field. This is the function:
<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_width']和$ _POST ['screen_height']的值时,什么都没有发生.但是当我重新提交它时,值就存在了.有人可以告诉我我在做什么错吗?如果您有其他解决方案,也将不胜感激.
The form submits to the same page. When I click 'submit' and try to echo the value of $_POST['screen_width'] and $_POST['screen_height'], nothing happens. But when I re-submit it, the values are there. Can someone tell me what am I doing wrong? If you have an alternative solution, it would be equally appreciated.
推荐答案
我自己解决了这个问题,这只是在加载元素后调用函数的问题.
Solved it myself, it was just a matter of calling the function after loading the element.
这篇关于使用Javascript设置隐藏字段值;使用PHP post方法发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!