简体   繁体   English

使用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. 我需要获取用户的屏幕分辨率并将其保存在我的数据库中。

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. 我正在做的是:我有一个名为的Javascript函数,它获取信息并将值传递给隐藏的表单字段。 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>

And the form: 以及形式:

<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>

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. 当我单击“提交”并尝试回显$ _POST ['screen_width']和$ _POST ['screen_height']的值时,什么都没有发生。 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. 如果您有替代解决方案,也将不胜感激。

have you checked firebug's output? 您检查萤火虫的输出了吗? maybe there are some errors? 也许有一些错误? try alert sth in your function, maybe it isn't called after initial page load? 尝试在您的函数中发出警报,也许在初始页面加载后未调用它? try putting function call in submit button's onClick or form's onSubmit event. 尝试将函数调用放入提交按钮的onClick或表单的onSubmit事件中。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM