首页 > 解决方案 > 保持选中表中的选定选项

问题描述

我的问题是我有一个页面,您可以在其中向表格添加行,并且表格中有一个下拉菜单。

对于下拉列表,它应该自动保留选定的索引,并且下一个添加的行应该与上面的行选择相同的选项。这可行,但是上面的行不会保留所选选项并重置为第一个选项。

<?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>

标签: phphtmlhtml-table

解决方案


推荐阅读