无法从PHP中的选择下拉列表中检索多个值

时间:2017-10-17 02:25:04

标签: php

问题是我只能从选择下拉列表中检索一个值,即使我选择了2,试图在这里查看类似的问题,但似乎没有一个对我有效。 有什么想法吗?感谢

if (isset($_POST['submit'])){

$smsorcall = $_POST['smsorcall'];

foreach($smsorcall AS $index => $smsorcall ) {

echo "$smsorcall";}

}
  <form action="newpatient.php" method="post">
  
   <p>Reminder Preference: *</p> 
		  <select name="smsorcall[]" style="width: 250px" class="form-control" multiple>
		
          <option value="SMS">SMS</option>
          <option value="Call">Call</option>
          <option value="Email">Email</option>
          
          </select>
          
          
           <button type="submit" name="submit">Submit</button>

          </form>

我的代码

   <?php ob_start();
   session_start();

  include('connect-db.php');

  if (isset($_POST['submit']))
  {


  $patientid = $_POST['patientid'];

  $smsorcall = $_POST['smsorcall'];

  foreach($smsorcall AS $index => $smsorcall ) {

  echo "$smsorcall";}



 $_SESSION['smsorcall'] = $smsorcall;

在另一个html页面中,我回显$ _SESSION ['smsorcall']来显示结果

1 个答案:

答案 0 :(得分:1)

您只保存会话的最后一个值 - 您应该从POST保存整个数组,然后当您想要从会话中获取值循环数组时,例如:

if (isset($_POST['submit']))
{
    // save the WHOLE ARRAY of selected options to the session
    $_SESSION['smsorcall'] = $_POST['smsorcall'];

    /* Any more code here... */
}

在您的另一页上:

if (isset($_SESSION['smsorcall']))
{
    // Get the array of all selected options from the session
    $smsorcall = $_SESSION['smsorcall'];

    // loop through the array to process each option one at a time
    foreach($smsorcall AS $index => $option ) {
        // Do whatever you want with each option here, e.g. to display it:
         echo $option;
    }
}