如何使用选择和提交按钮重定向

时间:2017-10-10 20:32:32

标签: php html

我使用php填充选项列表,然后想要使用提交并将我带到另一个页面...如何查看下拉列表中选择的内容,提交并带我到那个页面?

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

2 个答案:

答案 0 :(得分:0)

首先,您需要为您的选项元素命名。

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

然后在test.php文件中,您可以使用:

访问select
$selectOption = $_POST['some_Name'];

答案 1 :(得分:0)

诀窍在于,当您通过$_POST发送选项时,您需要为 <select> 元素添加 name < / strong>属性(不是每个<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>

然后,您可以使用以下内容检索选定的<option>

$_POST['description']

希望这有帮助! :)