HTML / Php 表单 - 如何将可选的、未选择的下拉列表保存为空/空而不是选项?
我正在通过 cTools 向导(主要是 php)为 Drupal 6 构建表单,并且我有一些可选的选择框。我的选项设置如下:
0|选择
1|选项 1
2|选项 2
3|选项 3
4|选项 4
或者在某些选项上,例如年份或州选择,我使用 array_unshift 将“选择”附加到构建的列表的前面动态或从其他地方引用。
当没有用户输入时,这些选择框返回“Select”、0 或 -1 值(当我希望它们什么都不返回时)。
我对 PHP 还很陌生,所以我可能没有以正确的方式问这个问题,因为我根本没有发现任何关于这似乎应该是一个相当常见的问题的信息。任何人都知道如何解决这个问题,或者如何更好地表达我的问题以找到答案?谢谢!
I'm building forms for Drupal 6 through the cTools wizard (which is primarily php), and I have a few select boxes that are optional. I have my options set up like so:
0|Select
1|option 1
2|option 2
3|option 3
4|option 4
Or on some, like a year or state select, I'm using array_unshift to append a "Select" to the front of a list that is built dynamically or referenced from elsewhere.
When there is no user input, these select boxes return values of "Select", 0, or -1 when I want them to return nothing at all.
I'm pretty new to PHP, so I'm probably not asking this in the right way because I haven't found anything at all about what seems like it should be a fairly common issue. Anyone have any idea how do to this, or how to better phrase my issue to find answers? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于该字段已发布,因此您将始终在
$_POST
中收到某种字符串值。我通常提供一个空白值 (),这会在 PHP 端给我一个空白字符串。然后你可以(如果你不介意转换“0”-> false)做一些类似
取消该值的事情。
Because the field gets posted, you will always receive a string value of some sort in
$_POST
. I normally provide a blank value (<option value="">Select</option>
) which gives me a blank string on the PHP side. You can then (if you don't mind casting "0" -> false) do something liketo null out that value.