Reputation: 99
I'm trying to access the value of a select value with php. The select gets it's options from a database.
<form action="#" method="post">
Product:<br>
<input type="text" name="product"><br>
Category:<br>
<select name="category">
<?php
$sql = mysqli_query($link, "SELECT name FROM inventory_category");
while ($row = $sql->fetch_assoc()){
echo "<option value=\"Category\">" . $row['name'] . "</option>";
}
?>
</select><input type="submit" name="submit"></form>
<?php
if(isset($_POST['submit'])){
$name = $_POST['product'];
$category = $_POST['category'];
echo $name;
echo '<br>';
echo $category;?>
I get the expected result for product
but not for category
which displays as Category
instead of the option selected.
I'm using this as a debug but ultimately I will be storing these chosen values to the database. Is there any difference to how to handle both situations?
Thanks in advance.
Upvotes: 0
Views: 714
Reputation: 15131
Replace the value
with the data you want:
echo "<option value=\"".$row['name']."\">" . $row['name'] . "</option>";
Upvotes: 4