简体   繁体   中英

How to redirect using select and submit button

I'm using php to populate an option list, and then want to use a submit to and bring me to another page...how can I check what was selected in the dropdown list,submit and bring me to that page?

 form action="/test.php" method="post">
    <select>
    <option value hidden>All</option>
    <?php

    $connection = mysqli_connect("127.0.0.1","root","");
    mysqli_select_db($connection,"test");
    $result = mysqli_query($connection,"select description from categories");
    while($row = mysqli_fetch_array($result))
    {   
    print "<option value = 
    ".$row['description'].".php".">".$row['description']."</option>";
    }

    ?>
    </select>
    <button type = "submit" form= "menu" value="Submit">Filter</button>`

first you need to give your option element a name.

form action="/test.php" method="post">
    <select>
    <option value hidden>All</option>
    <?php

    $connection = mysqli_connect("127.0.0.1","root","");
    mysqli_select_db($connection,"test");
    $result = mysqli_query($connection,"select description from categories");
    while($row = mysqli_fetch_array($result))
    {   
    print "<option name='some_Name' value = 
    ".$row['description'].".php".">".$row['description']."</option>";
    }

    ?>
    </select>
    <button type = "submit" form= "menu" value="Submit">Filter</button>`

then in your test.php file you can access the select using:

$selectOption = $_POST['some_Name'];

The trick is that when you're sending options via $_POST , you need to give the <select> element a name attribute (not each <option> ):

<form action="/test.php" method="post">
  <select name="description">
    <option value hidden>All</option>
    <?php
    $connection = mysqli_connect("127.0.0.1","root","");
    mysqli_select_db($connection,"test");
    $result = mysqli_query($connection,"select description from categories");
    while($row = mysqli_fetch_array($result)) {   
      print "<option name='some_Name' value = 
      ".$row['description'].".php".">".$row['description']."</option>";
    }
    ?>
  </select>
  <button type="submit" form="menu" value="Submit">Filter</button>
</form>

Then you can retrieve the selected <option> with:

$_POST['description']

Hope this helps! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM