merhaba kategorileri sayfada açılan menüde göstermek istiyorum ,aşağıdaki resimde olduğu gibi ,
Kutucuğa tıkladığımda veri tabanından kategori listesi gelmiyor . Kodlara bakınca bir hata da bulamadım ,çok uğraştığım içinde olabilir . Fikir verebilirseniz sevinirim .
index.php dosyası
<?php
include "config.php";
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.3/css/bootstrap.min.css" integrity="sha512-oc9+XSs1H243/FRN9Rw62Fn8EtxjEYWHXRvjS43YtueEewbS6ObfXcJNyohjHqVKFPoXXUxwc+q1K7Dee6vv9g==" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
</head>
<body>
<div class="container">
<div class="row mt-5">
<div class="col-md-8 offset-2 mt-5">
<div class="card mt-5">
<div class="card-header bg-primary text-white">
<h4><b>category</b></h4>
</div>
<div class="card-body">
<form>
<div class="form-group">
<label for="CATEGORY-DROPDOWN">Category</label>
<select class="form-control" id="category-dropdown">
<option value="">Select Category</option>
<?php
$result = mysqli_query($conn,"SELECT * FROM categories where sub_cat_id = 0");
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row['id'];?>"><?php echo $row["category"];?></option>
<?php
}
?>
</select>
</div>
<div class="form-group">
<label for="SUBCATEGORY">Sub Category</label>
<select class="form-control" id="sub-category-dropdown">
<option value="">Select Sub Category</option>
</select>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
<script>
$(document).ready(function() {
$('#category-dropdown').on('change', function() {
var category_id = this.value;
$.ajax({
url: "get-subcat.php",
type: "POST",
data: {
category_id: category_id
},
cache: false,
success: function(result){
$("#sub-category-dropdown").html(result);
}
});
});
});
</script>
</body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
get-subcat.php dosyası
<?php
include "config.php";
$category_id = $_POST["category_id"];
$result = mysqli_query($conn,"SELECT * FROM categories where sub_cat_id = $category_id");
?>
<option value="">Select Sub Category</option>
<?php
while($row = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $row["id"];?>"><?php echo $row["category"];?></option>
<?php
}
?>
------------------------------------------------------------
config.php dosyası
<?php
$servername = 'localhost';
$username = 'root'; // Username
$password = 'root'; // Password
$dbname = "db_name";
$conn = mysqli_connect($servername,$username,$password,"$dbname");
if(!$conn){
die('Could not Connect MySql Server:' .mysql_error());
}
?>
-----------------------------------------------