Hello, since i made a select box that if an option is selected a subcategory select box appears, I got problems with sending it to the db.
html code for the select boxes:
<div class="form-group">
<label>Kost categorie</label>
<select name="category" class="mainselect">
<option value="">Selecteer</option>
<option value="woning">Woning</option>
<option value="vervoer">Vervoer</option>
</select>
<select name="subcategory" class="subselect">
<optgroup label="woning">
<option value="huishouden">Huishouden</option>
<option value="huur">Huur</option>
</optgroup>
</select>
<select name="subcategory" class="subselect">
<optgroup label="Vervoer">
<option value="auto">Auto</option>
<option value="fiets">Fiets</option>
</optgroup>
</select>
</div>
the jquery script to show the second select box:
var $sub = $('select.subselect');
$('select').first().change(function() {
$sub.hide();
// If the first option is not selected
if (this.selectedIndex > 0)
$sub.eq(this.selectedIndex - 1).show();
});
and then the insert into db code:
$costname = $_POST['costname'];
$category = $_POST['category'];
$subcategory = $_POST['subcategory'];
$price = $_POST['price'];
$info = $_POST['info'];
$costdate = $_POST['costdate'];
$iduser = $_SESSION['id'];
//Insert into db
if($stmt = $conn->prepare("INSERT INTO costs (costname, category, subcategory, price, info, userid, costdate)
VALUES (?, ?, ?, ?, ?, ?, ?)")) {
$stmt->bind_param("sssdsis", $costname, $category, $subcategory, $price, $info, $iduser, $costdate);
$stmt->execute();
}
Somehow if i add the values everything is ok even the main category is fine but the subcategory is always the first value of the last select box in this case: auto.
I hope someone can help me i’ve been stuck on this for a day now