$ _POST禁用选择 [英] $_POST for disabled select
问题描述
<select class="txtbx1" name="country" disabled>
<option value='FR' >FRANCE</option><option value='CH' selected>SWITZERLAND</option>
</select>
上面的代码位于方法为post的表单内
the above code is inside a form whose method is post
但是echo $_POST['country']
却什么也没显示..另一方面,如果我从选择$_POST['country']
中取消禁用则显示正确的结果
but echo $_POST['country']
is showing nothing.. on the other hand if I remove disabled from select $_POST['country']
is showing the correct result
推荐答案
这是disabled
属性的工作方式.禁用表单控件后,提交表单时该值将被忽略,并且键不会出现在$_POST
(或$_GET
)中.
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
).
如果您希望该值出现在提交的数据中,但又不希望用户能够更改页面上的值(我想这是您要达到的目标),请使用readonly="readonly"
而不是disabled="disabled"
.
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"
.
编辑
<select>
元素没有readonly
属性.以上信息仍然适用于<textarea>
的 .
The <select>
element does not have a readonly
attribute. The above information still stands as it will work for <input>
s and <textarea>
s.
这里问题的解决方案是禁用选择并使用隐藏的输入将值发送回服务器-例如
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.
启用选择后:
<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" />
这篇关于$ _POST禁用选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!