My project right now has two dropdown menu. One is with color and the other is with the members of that color. The goal is to use JS so if I select "Red", only members in red show up on the second dropdown. I have my SQL table below and my script. I am not sure where to start so some help would be nice.
SQL:
+--------+--------------+
| Color | MemberName |
+--------+--------------+
| Red | Joe Bob |
| Red | Catherine |
| Blue | Tommy |
| Orange | John Razks |
| Black | Trevor Smith |
+--------+--------------+
+--------+
| Color |
+--------+
| Red |
| Blue |
| Orange |
| Black |
+--------+
PHP Code:
<!DOCTYPE html>
<html>
<head>
<title>Test Page</title>
<script src="jquery.js"></script>
</head>
<body>
<?php
function load_Color(){
$conn=mysqli_connect("#variable");
if(!$conn){ die("Connection Failed".myslqi_connect_error()); }
else{
$output='';
$sql = "SELECT * from Color order by Color ASC";
$result = mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($result)){
$output .= '<option value="'.$row["Color"].'">'.$row["Color"].'</option>';
}
return $output;
}
}
?>
<div class="formatbody" id="formatbody">
<div class="category_div" id="category_div">Color:
<select id="color" name="color">
<option value="">Select Color</option>
<?php echo load_Color(); ?>
</select>
</div>
<div class="sub_category_div" id="sub_category_div">Individual:
<select name="individual" id="individual">
<option value="">Select Individual</option>
</select>
</div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
$('#color').change(function(){
var Color = $(this).val();
$.ajax({
url: "fetch.php",
method: "POST",
data:{color: Color},
dataType: "text",
success: function(data)
{
$('#individual').html(data);
}
});
});
});
</script>
Fetch.PHP:
<html>
<body>
<?php
$conn=mysqli_connect("#Variable");
if(!$conn){ die("Connection Failed".mysqli_connect_error()); }
else{
$output='';
$sql = "SELECT MemberName from Members where Color = '".$_POST["color"]."' ORDER BY MemberName ASC";
$result = mysqli_query($conn, $sql);
$output = '<option value="">Select the Individual</option>';
while ($row=mysqli_fetch_array($result))
{
$output .='<option value="'.$row["MemberName"].'">'.$row["MemberName"].'</option>';
}
}
echo $output;
?>
</body>
</html>