dongzhila3786 2011-04-13 16:43
浏览 111
已采纳

确定是否选择了其他<option>,或者是否提交了具有空白值的默认“选择一个...”

I am adding a sort by option to my search page. I am using two select form input type, one for the field to order by, and one for ASC/DESC. I have the first option with value="" and text "Select one...".

<label for="sort[order_by_field]">Field</label><select name="sort[order_by_field]" id="combobox">
    <option value="">Select one...</option>
    <optgroup label="---">
        <option value="finding_incident_number"<?php echo ($field == 'finding_incident_number' ? ' selected="selected"': false); ?>>Incident #</option>
        <option value="finding_violation_type"<?php echo ($field == 'finding_violation_type' ? ' selected="selected"': false); ?>>Finding</option>

<label for="sort[order_by_direction]">Direction</label>
    <select name="sort[order_by_direction]">
    <option>Select one...</option>
    <option value="ASC"<?php echo ($dir == 'ASC' ? ' selected="selected"': false); ?>>Ascending</option>
    <option value="DESC"<?php echo ($dir == 'DESC' ? ' selected="selected"': false); ?>>Descending</option>
    </select>

After submitting my form, I check if isset($_POST['sort']). It is set. I also check for !empty. It is always !empty. I want to know if the user actually made a selection. Is there a way to set this up so that I don't have to check on individual array values, i.e. isset($_POST['sort']['order_by_field'])? I would like to have a dynamic number of addable/removable sort by fields in the future.

I suppose I could use an optiongrp label, but then that wouldn't be semantic markup would it? I guess it would work though.

From a broader perspective, is putting "Select One..." a good practice for a form dropdown?

  • 写回答

1条回答 默认 最新

  • dongzhi5846 2011-04-13 16:47
    关注

    The select field always will be set in $_POST

    you should check by

    if ($_POST['sort_by']['field_name'] !='')
    

    OR

    if ($_POST['sort_by']['field_name'] != null)
    

    and this is always a good practice to check variable before using them.

    putting "Select One..." is a good practice nothing bad with this.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办