kategori
-id (AI,INT)
-adi (VARCHAR)
tbl_product
-id (AI,INT)
-adi (VARCHAR)
-kategori_id(INT)
index.php dosyası;
<script> src="https://code.jquery.com/jquery-3.4.1.js"
integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU="
crossorigin="anonymous"></script>
<script></script>
<script>
$("#urun").remoteChained("#kategori", "notbil-ajax.php");
</script>
notbildb.php dosyası;
error_reporting(0);
// VERİTABANI BAĞLANTISI
try {
$nese = new PDO("mysql:host=localhost;dbname=ajans1", "root", "");
$nese->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch ( PDOException $e ){
print $e->getMessage();
}
$nese->query("SET CHARACTER SET utf8"); // UTF-8 KARAKTER DESTEĞİ
date_default_timezone_set('Europe/Istanbul');
?>
notbil-ajax.php dosyası;
include("notbildb.php");
if(isset($_GET['kategori'])){
$urunid=$_GET['kategori'];
if($urunid>0){
$sor=$nese->query("SELECT *FROM tbl_product WHERE kategori_id='".$urunid."' ORDER BY id ASC");
$list='{"0":"Ürün Seçiniz",';
while($urundizi=$sor->fetch(PDO:: FETCH_ASSOC)){
$list.='"'.$urundizi['id'].'":"'.$urundizi['adi'].'",';
}
$list=substr($list,0,-1);
$list.="}";
echo $list;
}
}
?>
notbil-chained.js dosyası
(function($) {
$.fn.remoteChained = function(parent_selector, url, options) {
return this.each(function() {
var self = this;
var backup = $(self).clone();
$(parent_selector).each(function() {
$(this).bind("change", function() {
var id = $(this).attr("id");
if(id==''){
$("#urun").attr("disabled", true);
}
var data = {};
$(parent_selector).each(function() {
var id = $(this).attr("id");
var value = $(":selected", this).val();
data[id] = value;
});
$.getJSON(url, data, function(json) {
if(id=='kategori'){
$("#urun").attr("disabled", false);
}
$("option", self).remove();
for (var key in json) {
if (!json.hasOwnProperty(key)) {
continue;
}
if ("selected" == key) {
continue;
}
var option = $("").val(key).append(json[key]);
$(self).append(option);
}
$(self).chkategoridren().each(function() {
if ($(this).val() == json["selected"]) {
$(this).attr("selected", "selected");
}
});
$(self).trigger("change");
});
});
$(this).trigger("change");
});
});
};
$.fn.remoteChainedTo = $.fn.remoteChained;
})(jQuery);