尝试$ _POST多个选定的选项,但没有获得任何选定的选项

时间:2014-08-21 22:45:32

标签: php forms

这是一个表单,其中一个选项允许多个选项(name =“mission []”)。虽然我已经阅读了有关在名称中使用括号的一些注释,但我仍然在表单的该部分中留空。有什么想法吗? (上下文:使用PHPMailer和Gmail作为SMTP)

if ($_SERVER["REQUEST_METHOD"] == "POST") {
$firstname = trim($_POST["firstname"]);
//more of these

$mission = trim($_POST["mission"]);
$date = trim($_POST["date"]);
$time = trim($_POST["time"]);

//some other code here

//message in email to be sent

$email_body = "";
$email_body = $email_body . "What I am trying to improve at our clinic: " . $mission . "<br>" ;

以下是表单中的html代码:

<label for="mission" class="col-sm-3 control-label">What are you trying to improve at your clinic?</label>
<div class="col-sm-9">
   <select name="mission[]"  multiple="multiple" class="form-control" style="max-height:75px">
      <option disabled>(Use the Shift key to select more than one)</option>
      <option  value="Decrease noshows"> Decrease patient no-shows</option>
      <option  value="increase # of patients completing a full episode of care"> Increase number of patients completing care.</option>
      <option  value="Improve clinic reviews"> Improve my clinic's reviews.</option>
      <option  value="Increaes physician referrals"> Increase physician referrals.</option>
      <option  value="increase patient referrals"> Increase patient referrals.</option>
      <option  value="improve overall patient satisfaction"> Improve overall patient satisfaction.</option>
   </select>
</div>

解决方案:我最后使用了以下代码:

$mission_vals = "";
foreach ($mission as $missionselected){
  $mission_vals .= $missionselected . ", "; 
}

1 个答案:

答案 0 :(得分:0)

因为您在调用mission[]时命名了您的选择$_POST["mission"] PHP正在返回一个数组,而不是字符串。

您需要以下内容:

$missionArray = $_POST["mission"];

for ($i=0; $i < count($missionArray); $i++)
{
    $missionArray[$i] = trim($missionArray[$i]);
}

编辑: - 修剪部分,更有效:

$missionArray = array_map('trim', $missionArray);