php - 保持选中表中的选定选项
问题描述
我的问题是我有一个页面,您可以在其中向表格添加行,并且表格中有一个下拉菜单。
对于下拉列表,它应该自动保留选定的索引,并且下一个添加的行应该与上面的行选择相同的选项。这可行,但是上面的行不会保留所选选项并重置为第一个选项。
<?php
session_start();
require_once '../../connect/db.php';
include '../../functions/function.php';
if(!isset($_SESSION['row'])){
$_SESSION['row'] = 1;
}
$def = 4;
$pass = 5;
$run = 3;
if(isset($_POST['add'])){
$_SESSION['row']++;
}
if(isset($_POST['reset'])){
$_SESSION['row']=1;
}
?>
<html>
<head>
<title>Insert Plays</title>
<link rel="stylesheet" href="../../style/style.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="navbar">
<a href='home.php'>My Teams</a>
<a class="active" href=''>Insert Plays</a>
<a href='home.php?show=manage'>Manage Teams</a>
<a href='edit_profile.php'>Edit Profile</a>
<a class='log' href='../../index.php'>Logout</a>
</div>
<div class="content">
<form action="" method="post">
<table border="1">
<?php
$row = $_SESSION['row'];
echo $row;
$output = "";
for($i = 0;$i<$row;$i++){
$output .= "<tr>";
if ($i == $_SESSION['row']-1){
echo "Testaaaa";
$test = "playType".($i-1);
switch($_POST[$test]){
case 'pass':
$output .= "<td><select name='playType".$i."'>
<option selected value='pass' name='type'>Pass</option>
<option value='run' name='type'>Run</option>
<option value='def' name='type'>Def</option>
</select></td>";
break;
case 'run':
$output .= "<td><select name='playType".$i."'>
<option value='pass' name='type'>Pass</option>
<option selected value='run' name='type'>Run</option>
<option value='def' name='type'>Def</option>
</select></td>";
break;
case 'def':
$output .= "<td><select name='playType".$i."'>
<option value='pass' name='type'>Pass</option>
<option value='run' name='type'>Run</option>
<option selected value='def' name='type'>Def</option>
</select></td>";
break;
};
}
else{
$output .= "<td><select name='playType".$i."'>
<option value='pass' name='type'>Pass</option>
<option value='run' name='type'>Run</option>
<option value='def' name='type'>Def</option>
</select></td>";
}
$output .= "</tr>";
}
echo "$output";
?>
</table>
<input type="submit" value="Add Row" name="add"><br>
<input type="submit" name="reset" value="Reset">
</form>
</div>
</body>
</html>
解决方案
推荐阅读
- google-apps-script - Google 表格的解决方法访问权限
- python - 访问列表中具有特定属性的对象
- gradle - Gradle 的 Groovy 语法:项目
- c - 为什么我们需要在 C 中为数组使用 malloc() 或任何其他动态内存?
- azure-active-directory - Azure IoTHub 用于访问遥测和心跳数据的最低特权角色
- c - 运行 eclipse CDT 名称样式检查无头
- html - 发送短信链接正文不显示带有等号“=”的网址
- python - 当作为参数传递或返回时,Numpy 如何处理内存分配?
- c# - c#如何在包含许多数据表的数据集中查找重复记录?
- php - 无法使用 PHP exec 运行 gsutil