I have a little question about some code I found online, the main issue is that I want to to have 2 dropdowns, the first dropdown looks like this:
<select name="member_group">
<option value="0">Vælg en gruppe først</option>
<?php
$sql = "SELECT * FROM member_groups WHERE company = '$row_cp[id]' ORDER BY id DESC";
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
print '<option rel="' . $row[id] . '" value="' . $row[id] .'">- ' . $row[name] . '</option>';
}
}
?>
</select>
In this section, I want the different groups I've created, to appear directly from the database.
Let's say we have 2 groups.
ID: 1 - Basic ID: 2 - Advanced
If I select ID: 2 - Advanced, I want in the next dropdown, only to show prices that are connected to ID: 2, all that happens in the database also, therefore my 2'nd dropdown would look like this.
<select name="products" class="cascade">
<?php
$sql = "SELECT * FROM products WHERE company = '$row_cp[id]' AND available_for = '$_POST[member_group]' ORDER BY id DESC";
$result = $conn->query($sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
print '<option class="' . $_POST[member_group] . '" value="' . $visgg[id] .'">- ' . $visgg[name] . '</option>';
}
}
?>
</select>
My issue here is, that in order only to show the ones that are linked to ID: 2, I must create this in SQL query
available_for = '$_POST[member_group]'
But since everything is happening in the same form, I don't get a value from $_POST[member_group].
I've used this code, I found on a forum
<script>
$(document).ready(function(){
var $cat = $('select[name=member_group]'),
$items = $('select[name=products]');
$cat.change(function(){
var $this = $(this).find(':selected'),
rel = $this.attr('rel'),
$set = $items.find('option.' + rel);
if ($set.size() < 0) {
$items.hide();
return;
}
$items.show().find('option').hide();
$set.show().first().prop('selected', true);
});
});
</script>
<style type="text/css">
.cascade {
display: none;
}
</style>
The demo code I downloaded, worked fine on jsfiddle, so I guess my question is how can I get the $_POST[member_group]
to the text dropdown, perhaps with some jQuery?
I always get great help here, so I hope you can help me out :o)