Skip to content
PHP Logo

How can a beginner approach creating a dropdown menu in PHP for redirecting to different pages?

To create a dropdown menu in PHP for redirecting to different pages, you can use HTML form elements along with PHP to handle the redirection. You can create a form with a dropdown menu containing the page options and use PHP to redirect to the selected page upon form submission.

php
<form action="redirect.php" method="post">
    <select name="page">
        <option value="page1.php">Page 1</option>
        <option value="page2.php">Page 2</option>
        <option value="page3.php">Page 3</option>
    </select>
    <input type="submit" value="Go">
</form>
```

In the `redirect.php` file, you can use PHP to redirect to the selected page based on the form submission.

```php
<?php
if(isset($_POST['page'])) {
    $page = $_POST['page'];
    header("Location: $page");
    exit();
}