I am using wampserver and its php, I have coursetable with 2 primary keys, these are subject and course. Subjects can be like math, cs (computer science) etc. Courses can be like 306, 307 etc, but both of them are varchar. I want to create 3 drop down list which 2 of them are linked. For example, if I select cs in first drop down, second one can be 300, 306 or 307, but if I select math subject in first dropdown list courses can be 203 or 306. The third dropdown list is grades and same in everywhere. The problem is I cannot get subject and course data like https://www.youtube.com/watch?v=8UMCg2KQcsM video or Dynamic dropdown list link. Can anyone help me to solve that problem.
connection.php
<?php
$con = mysqli_connect('localhost', 'root', '123456', 'inputdatabase');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect:".mysqli_connect_errno();
} ?>
getdata.php
<?php
include_once "connection.php"
if(!empty($_POST["course"])){
$course = $_POST["course"];
$query = "SELECT * FROM coursetable WHERE subject = $course";
$result = mysqli_query($con, $query);
foreach($result as $coursetable){
?>
<option value="<?php echo $coursetable["course"]; ?>"></option>
<?php
}
}?>
one part of html - I added connection.php, it is not problem
<select name="subject" onchange="getId(this.value);">
<option value="">Select course</option>
<?php
$query = "SELECT * FROM coursetable";
$results = mysqli_query($con, $query);
foreach ($results as $coursetable){
?>
<option value="<?php echo $coursetable["subject"];?>"></option>
<?php
}
?>
</select>
<div class ="course">
<select name="course" id="courseList">
<option value=""></option>
</select>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">
</script>
<script>
function getId(val){
$.ajax({
type: "POST",
url: "getdata.php",
data : { course : val },
success: function(data){
$("#courseList").html(data);
}
})
}
</script>