php - 重定向回起始页面时如何显示在下拉菜单中选择的原始名称?
问题描述
我想返回到我原来的索引页面并让下拉菜单显示最初选择的名称。然后,我想选择另一个学生并再次执行任务。
目前,我能够:
- 从下拉菜单中选择学生并将所选学生提交到另一个页面,
- 更新学生的特征并重定向回原始页面。
我可以在下拉菜单中显示学生姓名,但是当我选择一个新学生时,我的选择正上方的学生被选中。
第 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
}
}
}
解决方案
我通过更改此代码解决了这个问题
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>";}
}
推荐阅读
- python - 使用 Sci-Kit 的 Count Vectorizer 转换输入以仅匹配词汇表中的确切单词
- android - android中的同步改造请求
- r - 亚美尼亚语的 RMarkdown 和针织
- c# - C#在具有容差的图像中查找图像
- php - `preg_split` 在什么情况下可能返回 `false`
- python - Python:如何从请求标头中提取值?
- python-3.x - 运行 keras model.fit_generator 后需要帮助纠正错误
- firefox - Heroku 托管页面抛出错误 - navigator.mediaDevices 未定义
- python-3.x - python中连接失败时如何重新连接到websocket
- php - AngularJS 反序列化 PHP 字符串