php $_POST 禁用选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7244728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$_POST for disabled select
提问by infinityskyline
<select class="txtbx1" name="country" disabled>
<option value='FR' >FRANCE</option><option value='CH' selected>SWITZERLAND</option>
</select>
the above code is inside a form whose method is post
上面的代码在一个方法是 post 的表单中
but echo $_POST['country']
is showing nothing.. on the other hand if I remove disabled from select $_POST['country']
is showing the correct result
但echo $_POST['country']
什么也没显示……另一方面,如果我从 select 中删除 disabled$_POST['country']
会显示正确的结果
回答by DaveRandom
This is how the disabled
attribute works. When a form control is disabled, the value will be ignored when the form is submitted and the key will not be present in $_POST
(or $_GET
).
这就是disabled
属性的工作方式。当表单控件被禁用时,提交表单时该值将被忽略,并且键不会出现在$_POST
(或$_GET
)中。
If you want the value to be present in the submitted data, but you don't want the user to be able to change the value on the page (which I imagine is what you are trying to acheive) use readonly="readonly"
instead of disabled="disabled"
.
如果你想要的值存在于提交的数据,但你不希望用户能够更改页面上的值(我想是你想acheive什么)使用readonly="readonly"
代替disabled="disabled"
。
EDIT
编辑
The <select>
element does not have a readonly
attribute. The above information still stands as it willwork for <input>
s and <textarea>
s.
该<select>
元素没有readonly
属性。以上信息仍然有效,因为它会为工作的<input>
S和<textarea>
秒。
The solution to your problem here would be to disable the select and use a hidden input to send the value back to the server - e.g.
您的问题的解决方案是禁用选择并使用隐藏输入将值发送回服务器 - 例如
When the select is enabled:
启用选择时:
<select class="txtbx1" name="country">
<!-- options here -->
</select>
...and when it is disabled:
...当它被禁用时:
<select class="txtbx1" name="country_disabled" disabled="disabled">
<!-- options here, with appropriate value having `selected="selected"` -->
</select>
<input type="hidden" name="country" value="value_of_field" />
回答by Rocket Hazmat
This is the correct behavior. disabled
disables the element, and does not send it's value when a form is POSTed.
这是正确的行为。 disabled
禁用该元素,并且在发布表单时不发送它的值。
You can use JavaScript to un-disable the form before you submit it. Something like this (untested):
在提交表单之前,您可以使用 JavaScript 取消禁用表单。像这样的东西(未经测试):
document.getElementById('myForm').addEventListener('submit', function() {
for(var i = 0; i < this.children.length; i++){
var child = this.children[i];
if(child.disabled){
child.disabled = false;
}
}
});
回答by Carlos Eduardo da Fonseca
How your form tag looks like? You may have forgotten the method="post"
attribute...
你的表单标签是什么样的?你可能忘记了method="post"
属性...