表单已禁用选择选项作为默认选项而不发送任何要验证的内容
I have a form like so:
<select name="employment">
<option disabled="" selected="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
In PHP I am running through each of the $_POST variables and then checking if they have a value. If not I then add that field to the array for an error message.
The issue is that if I leave the default 'disabled' message selected nothing is passed through a post value so theres nothing for me to validate.
If I print_r my $_POST variable then it contains no 'employment' field unless I select an option.
How can I solve this?
我有一个这样的表格: p>
&lt; select name =“employment”&gt;
&lt; option disabled =“”selected =“”&gt; - 您的就业身份是什么 - &lt; / option&gt;
&lt; option value =“employ”&gt; Employed&lt; / option&gt;
&lt; option value =“self_employed”&gt; Self Employed&lt; / option&gt;
&lt; / select&gt;
code> pre>
在PHP中我正在运行 每个$ _POST变量然后检查它们是否有值。 如果不是,那么我将该字段添加到数组中以获取错误消息。 p>
问题是,如果我选择默认的“禁用”消息,则不会通过帖子值传递任何内容,因此没有任何内容 让我验证。 p>
如果我print_r我的$ _POST变量,那么除非我选择一个选项,否则它不包含'就业'字段。 p>
怎么能 我解决了这个问题? p>
div>
<select name="employment">
<option disabled selected>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
disabled attribute
- The disabled attribute is a boolean attribute.
- When present, it specifies that an option should be disabled
- A disabled option is unusable and un-clickable.
Syntax:
<select disabled>
Not
<select disabled="">
In case of XHTML, syntax differs like
<select disabled="disabled">
hidden attribute
So, If you want to validate it. Use hidden
in option.
<select name="employment">
<option hidden>-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
When, nothing got selected, then it will output as -- What is your employment status --
<?php
echo $Employment=$_POST['employment'];
?>
Output: -- What is your employment status --
So, Now you can easily use your validation in dropdown
For more info, click disabled attribute - W3 Schools
Try this:
<select name="employment">
<option disabled="" selected="" value ="">-- What is your employment status --</option>
<option value="employed">Employed</option>
<option value="self_employed">Self Employed</option>
</select>
PHP:
if(empty($_POST['employment'])){
return false;
}else {
// I got value;
}