0

在我的下拉框中,这是一个必填字段,但即使选择了某些内容,也会出现验证消息,

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

3 回答 3

1

试试这个:

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

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

于 2012-04-28T17:41:15.450 回答
1

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

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

只需使用前半部分,你会没事的。

于 2012-04-28T17:41:24.503 回答
0

您可能想从中删除selected<option value="" selected>尝试,因为如果是,$department==1那么您有两个selected可能导致错误的选项。

于 2012-04-28T17:38:20.673 回答