簡體   English   中英

重定向回起始頁面時如何顯示在下拉菜單中選擇的原始名稱?

[英]How to display original name selected in drop down menu when redirected back to the starting page?

我想返回到我原來的索引頁面並讓下拉菜單顯示最初選擇的名稱。 然后,我想 select 另一個學生並再次執行任務。

目前,我能夠:

  1. select 學生從下拉菜單中,並將所選學生提交到另一個頁面,
  2. 更新學生的特征並重定向回原始頁面。

我可以在下拉菜單中顯示學生姓名,但是當我 select 是一個新學生時,我選擇了我的選擇正上方的學生。

第 1 頁的代碼,帶有下拉菜單

 <div>
    <?php
     if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
        session_start();
        $pid = $_SESSION['stu_name_id_select'];
        echo $pid;
       }
     ?>
    <form name = "test" method="POST" action = "">
    <?php
    $select_student= $db->prepare("SELECT stu_name_id FROM students WHERE active = 'Yes' order by stu_name_id");    //sql select query
    $select_student->execute();
    echo "<select name='stu_name_id_select' onChange ='this.form.submit()'><option>Select</option>";   
    while ($result1=$select_student->fetch(PDO::FETCH_ASSOC)){
       echo "<option value = '".$pid."'";
      //echo "<option value = '".$result1['stu_name_id']."'";
      if (isset($_POST['stu_name_id_select']) && $_POST['stu_name_id_select'] == $result1['stu_name_id'])  echo 'selected="selected"';
         echo ">".$result1['stu_name_id']."</option>";  
         $pid = $resule1['stu_name_id'];
        }
    echo "</select>";       
   ?>
 <input type="submit" value="Add/Edit Records"  onclick="test.action='edit_records.php'; return true;" />
 <input type="submit" value="Add/Edit Accommodations"  onclick="test.action='accommodations.php'; return true;" />
 </form>
 </div>

edit_records 頁面的代碼,我將選定的學生返回到索引頁面。

if(!isset($errorMsg)){
  $stmt=$db->prepare("UPDATE learning_skills SET SCS = :fname WHERE stu_name_id = '".$id5."'"); //sql insert query                      
$stmt->bindParam(':fname',$fruit);  //bind parameter
if($stmt->execute()){
  $updateMsg=""; //execute query success message
  if (session_status() == PHP_SESSION_NONE  || session_id() == '') {
    session_start();}
 $_SESSION['stu_name_id_select']   = $id5;
 header("refresh:1;index.php"); //refresh 1 second and after redirect to index.php page
    }
  }
}

您在循環結束而不是開始時重新分配$pid ,因此每個學生實際上都有前一個學生的 PID。

只需移動$pid = $resule1['stu_name_id']; 作為while循環開始后的第一條指令。

while ($result1=$select_student->fetch(PDO::FETCH_ASSOC)){
  $pid = $resule1['stu_name_id'];
  echo "<option value = '".$pid."'";
  if (isset($_POST['stu_name_id_select']) && $_POST['stu_name_id_select'] == $result1['stu_name_id'])  echo 'selected="selected"';
  echo ">".$result1['stu_name_id']."</option>";  
}

我通過更改此代碼解決了這個問題

while ($result1=$select_student->fetch(PDO::FETCH_ASSOC)){
  echo "<option value = '".$pid."'";
  if (isset($_POST['stu_name_id_select']) && $_POST['stu_name_id_select'] == $result1['stu_name_id'])  echo 'selected="selected"';
  echo ">".$result1['stu_name_id']."</option>";  
  $pid = $result1['stu_name_id'];
}

  foreach ($result1 as $row){
     if(isset($_SESSION['stu_name_id_select']) && $_SESSION['stu_name_id_select'] == $row['stu_name_id'])
            {
            echo "<option selected = 'selected'  value=\"".$_SESSION['stu_name_id_select']."\">".$_SESSION['stu_name_id_select']."</option>";
            $_POST['stu_name_id_select'] = $_SESSION['stu_name_id_select'];
            unset($_SESSION['stu_name_id_select']);}
           else{    
             echo "<option value = '".$row['stu_name_id']."'";
             if (isset($_POST['stu_name_id_select']) && $_POST['stu_name_id_select'] == $row['stu_name_id']) echo 'selected="selected"';
             echo ">".$row['stu_name_id']."</option>";}
    } 

    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM