2017-10-17 154 views
0

問題是我只能從選擇下拉列表中檢索一個值,即使我選擇了2,也試圖在這裏查看類似的問題,但似乎沒有一個適用於我。 有什麼想法?由於無法從PHP中的選擇下拉中檢索多個值

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

 
$smsorcall = $_POST['smsorcall']; 
 

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

 
echo "$smsorcall";} 
 

 
}
<form action="newpatient.php" method="post"> 
 
    
 
    <p>Reminder Preference: *</p> 
 
\t \t <select name="smsorcall[]" style="width: 250px" class="form-control" multiple> 
 
\t \t 
 
      <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'],顯示結果

+0

你的代碼工作。你爲什麼認爲你只獲得一個價值? – FluffyKitten

+0

因爲我試過了,每當我選擇2個選項時,只顯示一個選項,我不知道爲什麼 – epiphany

+0

你的意思是顯示在你的回聲中,或者你是否在自己的代碼中以其他方式顯示它們?在你發佈的代碼中,它看起來像只有一個選項,因爲你沒有空格打印兩個,所以你看到的是「SMSEmail」而不是「SMS Email」。 – FluffyKitten

回答

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; 
    } 
} 
+0

仍然不能...所以$選項包含整個數組的值,例如SMSEmail,我可以將它存儲在一個會話,所以在另一個頁面,我可以訪問它?, – epiphany

+0

@tam不,'$ option'只是其中一個選項,我們通過循環訪問數組。 '$ smsorcall'是整個數組,所以我們將*保存到會話中。然後在您的其他頁面中,使用標題下方的代碼'在您的其他頁面上' – FluffyKitten

+0

@tam我已經爲代碼添加了更多評論來解釋 – FluffyKitten