确定是否有其他<选项>被选择,或者如果默认“选择一个...”已提交空白值选项>
我正在向我的搜索页面添加排序依据选项。我使用两种选择表单输入类型,一种用于排序依据的字段,一种用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
选择字段始终会在
$_POST
中设置,您应该通过
OR
检查,这始终是在使用变量之前检查变量的好习惯。
输入“选择一个...”是一种很好的做法,这没什么坏处。
The select field always will be set in
$_POST
you should check by
OR
and this is always a good practice to check variable before using them.
putting "Select One..." is a good practice nothing bad with this.