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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?