I’m making a website, where I should be able to delete some brands from a database, the brands is stored in categories.
In my form, I have a that echo’s out all the categories in my database.
The next should then contain all brands that is within’ my selected category.
My categories contains a categoryID which is the value of the in the first dropdown – The brands is having it’s own ID, but also having the categoryID, from the category they’re in.
How would you do this? – And can you give an example, it might need javascript or jquery, which is fine with me.
This is my code:
<form action="deleteBrand.php" method="post">
<fieldset class="delete">
<legend>Delete Brand</legend>
<div>
<label for="categoryid">Category:<br />
<?php
$stmt = $db->prepare("SELECT categoryid, category FROM categories");
$stmt->execute();
echo "<select name='categoryid' onchange='this.form.submit()'>";
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='{$row['categoryid']}'>{$row['category']}</option>";
}
echo "</select>";
?>
</label>
</div>
<div>
<label for="brand">Brand Name:<br />
<?php
$stmt = $db->prepare("SELECT brands.id, brand FROM brands WHERE brands.categoryid = ")
?>
</label>
</div>
<div>
<input type="submit" value="Delete Brand" />
</div>
</fieldset>
</form>
I’m still missing the last dropdown, but if I just can get the value from the selected item from the first dropdown, into a variable, it shouldn’t be hard for the rest of the code!
Thank you a lot
The below code is an example of how you can get the selected option value for a dropdown box. Depending on your needs, you can either preload your category id->brand mappings into a javascript variable that you can refer to, or use an ajax request to get the appropriate brands for your second combo box on demand, when the box is clicked.