Home > Enterprise >  how to redirect to a php file within an html select tag
how to redirect to a php file within an html select tag

Time:02-17

I am trying to make an inex.php file as a dashboard page that shows a selection of files that are part of the folder that index.php is part of. and when the user selects the file, I want it to redirect to that page. I am using xammp as a local webserver for this.

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

I am using a foreach to get all the files that are in the folder and populate them in a selection menu. I want to know how I can redirect to those files when I hit submit.

CodePudding user response:

Don't use a select to redirect, use a <a> tag.

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

// edit: wrong concat operand

CodePudding user response:

Your code is correct, you need only remove strtolower method from value, and use onchange on select tag.

Try it:

<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>
  • Related