HTML / Php 表单 - 如何将可选的、未选择的下拉列表保存为空/空而不是选项?

发布于 2024-12-18 17:24:39 字数 400 浏览 9 评论 0原文

我正在通过 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 技术交流群。

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

发布评论

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

评论(1

卸妝后依然美 2024-12-25 17:24:39

由于该字段已发布,因此您将始终在 $_POST 中收到某种字符串值。我通常提供一个空白值 (),这会在 PHP 端给我一个空白字符串。然后你可以(如果你不介意转换“0”-> false)做一些类似

<?php if(!$_POST['field']) $_POST['field'] = null; ?>

取消该值的事情。

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 like

<?php if(!$_POST['field']) $_POST['field'] = null; ?>

to null out that value.

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