如何获取所选的下拉项目

时间:2023-01-19 08:49:29

The below code is what I use to generate a dropdown menu from my database which I finally have working (new to php and mysql).

下面的代码是我用来从我的数据库生成一个下拉菜单,我最终工作(新的php和mysql)。

First of all, have I used the best method for doing so?

首先,我使用过最好的方法吗?

Secondly, I would then like to use the selected item to generate an image depending on the category selected once submitted using a button. How can I use the selected item in the drop down menu so that when the form is submitted it can generate the related image?

其次,我想使用所选项目生成图像,具体取决于使用按钮提交后选择的类别。如何在下拉菜单中使用所选项目,以便在提交表单时可以生成相关图像?

<?php
include_once ("conn.php");
$querydropdown = "SELECT DISTINCT category FROM categorytypes";
$resultcategories = $mysqli->query($querydropdown);

?>

<html>

<head>


</head>

<body>

<select id="categories" class="admindropdown">
    <option selected disabled>Choose one</option>
<?php
    while ($rowCerts = $resultcategories->fetch_assoc()) {
        echo "<option value=\"{$rowCerts['category']}\">";
        echo $rowCerts['category'];
        echo "</option>";
    }
?>

</select>

</body>

1 个解决方案

#1


1  

You need to add a name attribute to your selectelement.

您需要为您的选择元素添加名称属性。

For instance:

例如:

<select id="categories" class="admindropdown" name="my_dropdown">
    <option selected disabled>Choose one</option>
<?php
    while ($rowCerts = $resultcategories->fetch_assoc()) {
        echo "<option value=\"{$rowCerts['category']}\">";
        echo $rowCerts['category'];
        echo "</option>";
    }
?>

</select>

Then you can get the value using:

然后你可以使用以下方法获取值:

$dropdown_value = $_POST['my_dropdown'];

$ dropdown_value = $ _POST ['my_dropdown'];

Where my_dropdown is the value of the name attribute of your select element.

其中my_dropdown是select元素的name属性的值。

Also you have to wrap the above code with a form element.

您还必须使用表单元素包装上面的代码。

<form action="" method="POST">
/* CODE HERE */
</form>

You can leave the value of action attribute empty if you want to access it on the same page.

如果要在同一页面*问它,可以将action属性的值保留为空。

have fun!

玩的开心!

#1


1  

You need to add a name attribute to your selectelement.

您需要为您的选择元素添加名称属性。

For instance:

例如:

<select id="categories" class="admindropdown" name="my_dropdown">
    <option selected disabled>Choose one</option>
<?php
    while ($rowCerts = $resultcategories->fetch_assoc()) {
        echo "<option value=\"{$rowCerts['category']}\">";
        echo $rowCerts['category'];
        echo "</option>";
    }
?>

</select>

Then you can get the value using:

然后你可以使用以下方法获取值:

$dropdown_value = $_POST['my_dropdown'];

$ dropdown_value = $ _POST ['my_dropdown'];

Where my_dropdown is the value of the name attribute of your select element.

其中my_dropdown是select元素的name属性的值。

Also you have to wrap the above code with a form element.

您还必须使用表单元素包装上面的代码。

<form action="" method="POST">
/* CODE HERE */
</form>

You can leave the value of action attribute empty if you want to access it on the same page.

如果要在同一页面*问它,可以将action属性的值保留为空。

have fun!

玩的开心!