如果空消息一直出现下拉

时间:2012-04-28 17:33:58

标签: php mysql

在我的下拉框中,它是必填字段,但即使选择了某些内容,也会显示验证消息,

 if(empty($data['department']) || strlen($data['department']) )
    {
    $err[] = "ERROR - Please enter the department you are applying for.";
    }

是从

中提取的
$insert = "INSERT into users
`department`,  VALUES ( '$data[department] )";

HTML:

<td>Department<span class="required">*</span> </td>
        <td><select name="department"  id="department">

            <option value="" selected></option>

            <option <? if($department == '1') { ?> selected <? } ?>   
 value="1">1</option>

            <option <? if($department== '2') { ?> selected <? } ?> value="2">2   
  </option>

            <option <? if($department == '3') { ?> selected <? } ?> 
 value="3">3</option>


            </select>

3 个答案:

答案 0 :(得分:1)

试试这个:

if (empty($data['department']))
{
    $err[] = "ERROR - Please enter the department you are applying for.";
}

如果变量为空,则无需检查strlen

答案 1 :(得分:1)

这条线是矛盾的,并且总会返回true。

empty($data['department']) || strlen($data['department'])

只要使用上半场就可以了。

答案 2 :(得分:0)

您可能希望从selected移除<option value="" selected>并尝试,因为如果$department==1您有两个selected选项可能导致错误。