douyun4524 2014-08-21 22:45
浏览 32

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

This is a form and one of the options allows for more than one selection (name="mission[]"). Although I have read some remarks about using brackets in the name, I still keep getting a blank in that section of the form. Any thoughts? (context: Using PHPMailer and Gmail as 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>" ;

Here's the html code in a form:

<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>

Solution: I went with the following code in the end:

$mission_vals = "";
foreach ($mission as $missionselected){
  $mission_vals .= $missionselected . ", "; 
}
  • 写回答

1条回答 默认 最新

  • dpecb06062 2014-08-21 23:02
    关注

    Because you named your select mission[] when you call $_POST["mission"] PHP is returning an array, not a string.

    You need something like:

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

    Edit:-- The trimming part, more efficiently:

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

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据