首页 > 解决方案 > 重定向回起始页面时如何显示在下拉菜单中选择的原始名称?

问题描述

我想返回到我原来的索引页面并让下拉菜单显示最初选择的名称。然后,我想选择另一个学生并再次执行任务。

目前,我能够:

  1. 从下拉菜单中选择学生并将所选学生提交到另一个页面,
  2. 更新学生的特征并重定向回原始页面。

我可以在下拉菜单中显示学生姓名,但是当我选择一个新学生时,我的选择正上方的学生被选中。

第 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
    }
  }
}

标签: phphtml

解决方案


我通过更改此代码解决了这个问题

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>";}
    } 

    

推荐阅读