确定是否有其他<选项>被选择,或者如果默认“选择一个...”已提交空白值

发布于 2024-11-01 06:20:22 字数 1366 浏览 3 评论 0原文

我正在向我的搜索页面添加排序依据选项。我使用两种选择表单输入类型,一种用于排序依据的字段,一种用于 ASC/DESC。我有第一个选项,值为“”和文本“选择一个...”。

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

提交表单后,我检查是否 isset($_POST['sort'])。已定。我还检查了 !empty。它总是空的。我想知道用户是否真的做出了选择。有没有办法进行设置,以便我不必检查各个数组值,即 isset($_POST['sort']['order_by_field']) ?我希望将来有动态数量的可添加/可删除排序字段。

我想我可以使用 optiongrp 标签,但这就不是语义标记了,不是吗?我想它会起作用。

从更广泛的角度来看,输入“选择一个...”对于表单下拉列表来说是一个很好的做法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笔一画续写前缘 2024-11-08 06:20:22

选择字段始终会在 $_POST 中设置,

您应该通过

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

OR

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

检查,这始终是在使用变量之前检查变量的好习惯。

输入“选择一个...”是一种很好的做法,这没什么坏处。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文