v2.5.2
Giriş yap

Select idye göre seçenek çıkartma

wwx
366 defa görüntülendi

Yapmak istediğim paket seçilince paket_urun tablosun da paket_id ile eşleşen verileri select için de çıkartmak

<select>
     <?php 
       $query = $db->prepare("SELECT * FROM paket");
       $arama = $query->execute();
        if($query->rowCount()){
           foreach($query as $row){
       ?>
     <option>Paket Seç</option>
     <option><?=$row['paket_adi']?></option>
     <?php } } ?>
</select>
<select>
     <?php 
       $query = $db->prepare("SELECT * FROM paket_urun");
       $arama = $query->execute();
        if($query->rowCount()){
           foreach($query as $row){
       ?>
     <option>Paket Ürün Seç</option>  
     <option><?=$row['baslik']?></option>
     <?php } } ?>
</select>
dnzclk
880 gün önce

Bu şekilde yapabilirsin. Yalnız paket value değeri ID olacak Onu kodla kontrol edip veritabanına paket adı olarak kaydedersin.

    
<select id="paket" name="paket">
<option value="">--</option>
<?php 
$query = $db->prepare("SELECT * FROM paket");
$query->execute();
foreach($query as $row){
?>
<option value="<?=$row['paket_id']?>"><?=$row['paket_adi']?></option>
<?php } ?>
</select>

<select id="paketurun" name="paketurun">
<option value="">--</option>
<?php 
$query = $db->prepare("SELECT * FROM paket_urun");
$query->execute();
foreach($query as $row){
?>  
<option value="<?=$row['baslik']?>" class="<?=$row['paket_id']?>"><?=$row['baslik']?></option>
<?php } ?>
</select>

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script>
;(function($, window, document, undefined) {
"use strict";

$.fn.chained = function(parent_selector, options) {

return this.each(function() {

var child   = this;
var backup = $(child).clone();

$(parent_selector).each(function() {
$(this).bind("change", function() {
updateChildren();
});

if (!$("option:selected", this).length) {
$("option", this).first().attr("selected", "selected");
}

updateChildren();
});

function updateChildren() {
var trigger_change = true;
var currently_selected_value = $("option:selected", child).val();

$(child).html(backup.html());

var selected = "";
$(parent_selector).each(function() {
var selectedClass = $("option:selected", this).val();
if (selectedClass) {
if (selected.length > 0) {
if (window.Zepto) {
selected += "\\\\";
} else {
selected += "\\";
}
}
selected += selectedClass;
}
});

var first;
if ($.isArray(parent_selector)) {
first = $(parent_selector[0]).first();
} else {
first = $(parent_selector).first();
}
var selected_first = $("option:selected", first).val();

$("option", child).each(function() {

if ($(this).hasClass(selected) && $(this).val() === currently_selected_value) {
$(this).prop("selected", true);
trigger_change = false;
} else if (!$(this).hasClass(selected) && !$(this).hasClass(selected_first) && $(this).val() !== "") {
$(this).remove();
}
});

if (1 === $("option", child).size() && $(child).val() === "") {
$(child).prop("disabled", true);
} else {
$(child).prop("disabled", false);
}
if (trigger_change) {
$(child).trigger("change");
}
}
});
};

$.fn.chainedTo = $.fn.chained;

$.fn.chained.defaults = {};

})(window.jQuery || window.Zepto, window, document);

$(document).ready(function(){
$("#paketurun").chained("#paket");
});
</script>