【问题标题】:PHP form not POSTing multiple select fieldsPHP表单不发布多个选择字段
【发布时间】:2014-02-07 21:45:43
【问题描述】:

我的 PHP 表单没有发布表单中的多个选择选项。 这是我的 PHP:

if ($_POST['interested_in_testing'] != "")
{
    $_POST['interested_in_testing'] = filter_var($_POST['interested_in_testing'], FILTER_SANITIZE_STRING);
    $interested_in_testing = $_POST['interested_in_testing'];
}
else
    died();

添加到电子邮件:

<br/>Interested In Testing: " . $interested_in_testing ;

这是我的选择:

<label class="custom">Interested In Testing</label>
<select name="interested_in_testing[ ]" multiple="multiple">
  <option value="atas">ATAs</option>
  <option value="ip_phones">IP Phones</option>
  <option value="gateways">Gateways</option>
  <option value="ip_pbx">IP PBX</option>
</select>

它不会为此字段发送任何内容。我所有的单个字段都可以正常发送。有什么帮助吗?谢谢

【问题讨论】:

  • 它是一个数组,所以像对待一个数组一样对待它name="interested_in_testing[]" 所以!empty($_POST['interested_in_testing'][0]).. ..
  • 添加一个 print_r($_POST);在您的代码中(在 if 之前)并告诉我们显示的内容
  • 还有 [] 中没有空格
  • 我原来是没有空格的。我正在尝试:/ @Raphael Malie 因为它是一封电子邮件,所以它无法识别非 html
  • 这是一个选择。如果你想要多个值,为什么不使用复选框?

标签: php html post


【解决方案1】:

既然你要创建一个数组元素,你也应该考虑和另一端的数组一样。

if (is_array($_POST['interested_in_testing']) && !empty($_POST['interested_in_testing'])) {
    $interested_in_testing_val = array();
    foreach($_POST['interested_in_testing'] as $val) {
        $interested_in_testing_val[] = $val;
    }
    $interested_in_testing = implode(',', $interested_in_testing_val);
}

假设您希望值以逗号分隔。

【讨论】:

  • 选择的名称属性的括号内允许有空格。
  • 我是 PHP 新手,在我的 if 语句中将其视为数组是正确的。
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-13
相关资源
最近更新 更多