Reputation: 77
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).
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>
Upvotes: 2
Views: 66
Reputation: 424
You need to add a name
attribute to your select
element.
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'];
Where my_dropdown
is the value of the name
attribute of your select
element.
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.
have fun!
Upvotes: 1