如何重定向到 html select 标签内的 php 文件

how to redirect to a php file within an html select tag

我正在尝试制作一个 inex.php 文件作为仪表板页面,显示 index.php 所在文件夹中的一系列文件。当用户选择文件时,我希望它重定向到该页面。为此,我使用 xammp 作为本地网络服务器。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index</title>
</head>

<body>
    <h1>Admin portal</h1>

    <h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>




    <form action="" method="POST">
        <label for="pages">page:</label>
        <select name="pages" id="page">
            <?php
            foreach (glob("*.php") as $filename) {

                echo "<option value='strtolower($filename)'>$filename</option>";
            }
            ?>
        </select>
        <br><br>
        <input type="submit" value="Submit">



    </form>



</body>

</html>

我正在使用 foreach 获取文件夹中的所有文件并将它们填充到选择菜单中。我想知道如何在点击提交时重定向到这些文件。

不要使用 select 重定向,使用 <a> 标签。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>index</title>
</head>

<body>
    <h1>Admin portal</h1>

    <h3>select a php file relevent to a table in the database you would like to manage :index brings you right back :(</h3>


    <h1>pages</h1>
    <ul>
    <?php
        foreach (glob("*.php") as $filename) {
            echo "<li><a href=" . strtolower($filename) . ">" . $filename . "</a></li>";
        }
    ?>
    </ul>


</body>

</html>

// 编辑:错误的连接操作数

您的代码是正确的,您只需从值中删除 strtolower 方法,并在 select 标记上使用 onchange。

编辑,提交表单 时的第二个 Sol :

 <form action="" method="POST"  onsubmit="return submitForm()">
            <label for="pages">page:</label>
            <select name="pages" id="page"  >
                <?php
                foreach (glob("*.php") as $filename) {
                    $filename = strtolower($filename);
                    echo "<option value='$filename'>$filename</option>";
                }
                ?>
            </select>
            <br><br>
            <input type="submit"  id='submitBtn'  value="Submit">
    </form>

    <script type="text/javascript">
        function submitForm(){
                window.location.href =  document.getElementById("page").value;
                return false;
            }
    </script>

第一个sol,试试吧:

<select name="pages" id="page" onchange=" (window.location = this.value);">
        <?php
        foreach (glob("*.php") as $filename) {
            $filename = strtolower($filename);
            echo "<option value='$filename'>$filename</option>";
        }
        ?>
  </select>