duanmu2013 2017-10-17 02:25
浏览 44
已采纳

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

The issue is i can only retrieve one value from the select dropdown even i have selected 2, have tried to look at similar question here,but none seems to work for me. Any thoughts? Thanks

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>

My code

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

In another html page, i echo the $_SESSION['smsorcall'] to display the result

</div>
  • 写回答

1条回答 默认 最新

  • duanli6834 2017-10-17 02:57
    关注

    You are only saving the last value to the session - you should save the whole array from the POST, then when you want to get the values loop through the array from the session, e.g.:

    if (isset($_POST['submit']))
    {
        // save the WHOLE ARRAY of selected options to the session
        $_SESSION['smsorcall'] = $_POST['smsorcall'];
    
        /* Any more code here... */
    }
    

    On your other page:

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?