I need to add multiple dropdowns to my project and it also needs a dependent multiple dropdowns
The existing dropdown category should be changed to multiple dropdowns
Automatically they select values in its dependent subcategory multiple dropdowns it its possible ?
Current Single Choice Dependent Dropdown How To Make Multiple Choose Dependent Dropdown?
please help me
view page
<label for="category_id"><?php echo get_phrase('category'); ?> <span
class="text-danger">*</span></label>
<select class="form-control selectpicker" name="category_id" id="category_id" required multiple>
<option value="" hidden><?php echo get_phrase('select category'); ?></option>
<?php
$roles = $this->db->get('category_table')->result_array();
foreach($roles as $row):
?>
<option value="<?php echo $row['category_id'];?>">
<?php echo $row['category_name'];?>
</option>
<?php
endforeach;
?>
</select>
<label for="idsubcategory"><?php echo get_phrase('Sub_category'); ?>
<span class="text-danger"></span> </label>
<select class="form-control" name="idsubcategory" id="idsubcategory"
placeholder="subcategory" multiple>
<option value="" hidden><?php echo get_phrase('select subcategory'); ?></option>
<?php
$roles = $this->db->get('sub_category')->result_array();
foreach($roles as $row):
?>
<option value="<?php echo $row['idsubcategory'];?>">
<?php echo $row['subcategory_name'];?>
</option>
<?php
endforeach;
?>
</select>
ajax
$(document).ready(function()
{
$('#category_id').change(function() {
var category_id = $(this).val();
$.ajax({
url: "<?php echo site_url('admin/get_sub_category');?>",
method: "POST",
data: {
category_id: category_id
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].idsubcategory + '>' + data[i]
.subcategory_name + '</option>';
}
$('#idsubcategory').html(html);
}
});
return false;
});
});
controller
function get_sub_category(){
$category_id = $this->input->post('category_id',TRUE);
$data = $this->Crud_model->get_sub_category($category_id)->result();
echo json_encode($data);
}
model
function get_sub_category($category_id){
$query = $this->db->get_where('sub_category', array('category_id' => $category_id));
return $query;
}
<select id="select1">
<option value='opt1'>Option 1</option>
<option value='opt2'>Option 2</option>
<option value='opt3'>Option 3</option>
</select>
<select id="select2">
<select>
<select id="select3">
<select>
<script>
$('#select1').change(function() {
var category_id = $(this).val();
$.ajax({
url: "<?php echo site_url('controller/function_for_second_dropdown');?>",
method: "POST",
data: {
category_id: category_id
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].idsubcategory + '>' + data[i].your_option+ '</option>';
}
$('#select2').append(html);
}
});
$('#select2').change(function() {
var select2= $(this).val();
$.ajax({
url: "<?php echo site_url('controller/function_for_third_dropdown');?>",
method: "POST",
data: {
select2: select2
},
async: true,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
html += '<option value=' + data[i].your_option + '>' + data[i].your_option+ '</option>';
}
$('#select2').append(html);
}
});
</script>
Related
So the idea is to manipulate the value that is delivered by Ajax. This value should be divided by 20 and the result should be multiplied by the value from #nilai_hari input.
Here's my code
// trigger
<select class="form-control" id="id_anggota" name="id_anggota">
<option>-Pilih Karyawan</option>
<?php $no=1; foreach ($listAnggota as $l) {?>
<option value="<?php echo $l->id_anggota; ?>">
<?php echo $l->nama_lengkap; ?>
</option>
<?php $no++; }?>
</select>
// the effected
<select class="tunjangan_makan form-control total_tunjangan nominal" id="tunjangan_makan" name="tunjangan_makan"></select>
// another trigger
<input type="text" id="nilai_hari" name="nilai_hari" class="form-control">
And here's the script
$('#id_anggota').change(function() {
var id = $(this).val();
$.ajax({
url: "<?php echo base_url();?>hrd/penggajian/get_subpenggajian",
method: "POST",
data: {
id: id
},
async: false,
dataType: 'json',
success: function(data) {
var html = '';
var i;
for (i = 0; i < data.length; i++) {
if (data[i].flat_tunjangan == 1) {
html += '<option value="' + data[i].tunjangan_makan + '">' + number_format(data[i].tunjangan_makan, 0, '', '.') + '</option>';
} else {
$('#tunjangan_makan', '#nilai_hari').keyup(function() {
var nilai_hari = $("#nilai_hari").val(),
makan = ((data[i].tunjangan_makan / 20) * nilai_hari)
html += '<option value="' + makan + '">' + number_format(makan, 0, '', '.') + '</option>';
})
}
}
$('.tunjangan_makan').html(html);
}
});
});
So, the value of the data in which flat_tunjangan is 0 must be calculated from another input. Thanks in advance
You are doing it wrong. You should call other input onkey event and call same ajax request again and remove that onkey event inside $('#id_anggota').change().
$('#id_anggota').change(function(){
var id=$(this).val();
// perform ajax and get data
var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];
var html = '';
var i;
for(i=0; i<data.length; i++){
if(data[i].flat_tunjangan == 1){
html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';
} else {
var nilai_hari = $("#nilai_hari").val(),
makan = ((data[i].tunjangan_makan/20)*nilai_hari)
if (makan != '') {
html += '<option value="'+makan+'">'+makan+'</option>';
}
}
}
$('.tunjangan_makan').html(html);
});
$("#nilai_hari").keyup(function(){
var nilai_hari = $(this).val();
// again do ajax reqeust and fetch data using selected tunjangan_makan id
var id = $('#id_anggota').val();
var data = [{'flat_tunjangan': '1', 'tunjangan_makan': '20'}, {'flat_tunjangan': '0', 'tunjangan_makan': '30'}];
var html = '';
var i;
for(i=0; i<data.length; i++){
if(data[i].flat_tunjangan == 1){
html += '<option value="'+data[i].tunjangan_makan+'">'+data[i].tunjangan_makan+'</option>';
} else {
var nilai_hari = $("#nilai_hari").val(),
makan = ((data[i].tunjangan_makan/20)*nilai_hari)
if (makan != '') {
html += '<option value="'+makan+'">'+makan+'</option>';
}
}
}
$('.tunjangan_makan').html(html);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="form-control" id="id_anggota" name="id_anggota">
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>
<select class="tunjangan_makan form-control total_tunjangan nominal" id="tunjangan_makan" name="tunjangan_makan"></select>
<input type="text" id="nilai_hari" name="nilai_hari" class="form-control">
I have three tables like tbl_customers, tbl_items, tbl_customersOrders. I inserted the tbl_customers ID and tbl_items ID into the tbl_customersOrders which as a foreign key. Now think is based on first drop-down lists (here the tbl_customersOrders ID) I can fetch another two fields on other drop-down lists but .... I need the name of the customers which is id matched...also items name which is id matched Like:
what I tried:
Controller Code:
$data['gets_order'] = $this->box_property_model->getCustDataOrder();
Model Code:
public function getCustDataOrder()
{
$this->db->select('*');
$this->db->order_by('customer_orders_id', 'DESC');
$query = $this->db->get('tbl_customer_orders');
return $query->result();
}
View code:
<select name="nestedOrder" class="form-control" >
<option value="">--- Select Order ---</option>
<?php if(!empty($gets_order)) {
foreach($gets_order as $nst){
?>
<option value="<?php echo $nst->customer_orders_id; ?>"><?php echo $nst->new_id_generated; ?></option>';
<?php } } ?>
</select>
<label for="custNested">Customer Name-</label>
<select name="custNested" id="custNested" class="form-control" >
<option value="">Select ITEMS ID</option>
</select>
<label for="ItemsName">Items Name</label>
<select name="items_ids" id="getsitemsName" class="form-control" >
<option value="">Select ITEMS</option>
</select>
<label for="testing">Final ITEM Name</label>
<select name="org_itemsNameValue" id="org_itemsNameValue" class="form-control" >
<option value="">Select ITEMS Name</option>
</select>
JS CODE:
<script type="text/javascript">
$(document).ready(function() {
$('select[name="nestedOrder"]').on('change', function() {
var customID = $(this).val();
alert(customID);
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNested/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="custNested"]').empty();
$('select[name="custNested"]').append('<option value="0">--Select CUSTTT--</option>');
$.each(data, function(key, value) {
$('select[name="custNested"]').append('<option value="'+value.customer_id + '">'+ value.customer_name +'</option>');
});
}
});
$.ajax({
url: '<?php echo base_url();?>/Box_property/DropdownsAjaxNestedItems/'+customID,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="items_ids"]').empty();
$('select[name="items_ids"]').append('<option value="0">--Select --</option>');
$.each(data, function(key, value) {
$('select[name="items_ids"]').append('<option value="'+value.items_id +'">'+ value.items_id +'</option>');
});
}
});
});
$('select[name="items_ids"]').on('change', function() {
var items_ids = $(this).val();
alert(items_ids);
$.ajax({
url: '<?php echo base_url();?>/Box_property/itemsOrdersIDfetch/'+items_ids,
type: "GET",
dataType: "json",
success:function(data) {
$('select[name="org_itemsNameValue"]').empty();
$('select[name="org_itemsNameValue"]').append('<option value="0">--Select ITEMS Name--</option>');
$.each(data, function(key, value) {
$('select[name="org_itemsNameValue"]').append('<option value="'+value.items_id + '">'+ value.items_name +'</option>');
});
}
});
});
});
</script>
In Controller:
public function DropdownsAjaxNested($id) {
$result = $this->db->where("customer_id",$id)->get("tbl_customers")->result();
echo json_encode($result);
}
public function DropdownsAjaxNestedItems($id) {
$result = $this->db->where("customer_orders_id",$id)->get("tbl_items_order")->result();
echo json_encode($result);
}
public function itemsOrdersIDfetch($id){
$result = $this->db->where("items_id",$id)->get("tbl_items_master")->result();
echo json_encode($result);
}
I use an AJAX to display my categories. My problem is when I select a category, the dropdown come back always at this initial position
For example, if I click "Panneaux lumineux", the dropdown go back to "Select your categorie" automatically. in this case "Panneaux lumineux" is not selected.
Tk
<div class="col-md-5">
<script type="text/javascript">
jQuery(document).ready(function() {
$("#myAjax").on('click', function () {
$.ajax({
url: 'http://Admin/products_categories_ajax.php',
dataType: 'json',
success: function (data) {
//data returned from php
var options_html = '<option value="">-- Select your categorie --</option>';
for (index in data) {
var category_id = data[index]['id'];
var category_name = data[index]['text'];
options_html += '<option value="' + category_id + '">' + category_name + '</option>';
}
$('#category_id').html(options_html);
}
});
});
})
</script>
<div id="myAjax">
<select name="move_to_category_id" id="category_id">
<option value="">-- Select your categorie --</option>
<option value="0">Haut</option>
<option value="25">Panneaux lumineux</option>
<option value="23">Panneaux Signalétique</option>
<option value="20">Signalétique Camping</option>
<option value="22"> Barrières</option>
<option value="21"> Entrée</option>
</select>
<input name="current_category_id" value="20" type="hidden" />
</div>
</div>
The issue you have occurs, because your dropdown list is replaced with a new one when you click on the dropdown. Even if the dropdown is visually the same, it isn't in the DOM.
To solve it, you need to save the selected category ID and select it after the dropdown has been replaced.
<div class="col-md-5">
<script type="text/javascript">
jQuery(document).ready(function() {
$("#myAjax").on('click', function () {
var selectedCategory = $('select#category_id').val();
$.ajax({
url: 'http://Admin/products_categories_ajax.php',
dataType: 'json',
success: function (data) {
//data returned from php
var options_html = '<option value="">-- Select your categorie --</option>';
for (var index in data) {
var category_id = data[index]['id'];
var category_name = data[index]['text'];
options_html += '<option value="' + category_id + '"' + (category_id == selectedCategory ? ' selected' : '') + '>' + category_name + '</option>';
}
$('#category_id').html(options_html);
}
});
});
})
</script>
<div id="myAjax">
<select name="move_to_category_id" id="category_id">
<option value="">-- Select your categorie --</option>
<option value="0">Haut</option>
<option value="25">Panneaux lumineux</option>
<option value="23">Panneaux Signalétique</option>
<option value="20">Signalétique Camping</option>
<option value="22"> Barrières</option>
<option value="21"> Entrée</option>
</select>
<input name="current_category_id" value="20" type="hidden">
</div>
</div>
Above the
$.ajax function call
add
var selectedOptionVal = $('#category_id').val();
to save the value you selected.
then use that value to make the correct option selected when you recreate the html. So then try changing
for (index in data) {
var category_id = data[index]['id'];
var category_name = data[index]['text'];
options_html += '<option value="' + category_id + '">' + category_name + '</option>';
}
to
for (var index in data) {
var category_id = data[index]['id'];
var category_name = data[index]['text'];
var selectedString = category_id == selectedOptionVal ? ' selected="selected"' : '';
options_html += '<option value="' + category_id + '"' + selectedString + '>' + category_name + '</option>';
}
I tried to populate a select box based on the first but no result, no error but the second select box is not populate:
My html:
<div class="form-group">
<label>Firm:</label>
<select class="form-control marg-left-10" id="firm">
<?php if($all_firms): ?>
<?php foreach($all_firms as $firm): ?>
<option value="<?php echo $firm['id_firm']; ?>"><?php echo $firm['name_firm']; ?></option>
<?php endforeach; ?>
<?php endif; ?>
</select>
</div>
<div class="form-group">
<label>Administrator</label>
<select class="form-control marg-left-10" id="admin">
</select>
</div>
My php:
public function getAllFirms()
{
$this->load->database();
$all_firms = $this->db->query("SELECT *FROM firms");
if ($all_firms->num_rows())
{
$all_firms = $all_firms->result_array();
}
else
{
$all_firms = NULL;
}
return $all_firms;
}
public function getAdministrator($id)
{
$this->load->database();
$get_admin = $this->db->query("SELECT * FROM users,firms WHERE firms.id_firm = users.fk_firm and users.id = $id");
if ($get_admin->num_rows())
{
$get_admin = $get_admin->result_array();
}
else
{
$get_admin = NULL;
}
return $get_admin;
}
My script:
$("#firm").change(function() {
var selectedMark = $("#firm").val();
if (selectedMark != "") {
$.ajax({
type: "GET",
url: "<?php echo base_url() . 'user/getAdministrator'; ?>" + selectedMark,
success: function(data) {
$("#admin").html("");
$("#admin").append("<option value=''></option>");
$.each(data, function() {
$("#admin").append("<option value='" + this.id + "'>" + this.name + "</option>");
});
}
});
}
});
I use CodeIgniter as backend language, the first selectt box is populated but the second not.Please help me guys...HELP please!!!!! HELP ME GUYS!!Another idea to resolve this?
it looks like you've forgotten to add # when you calling 'admin' select. I.e. see 'success' section of your JS and change:
$('admin')...
to
$('#admin')...
you are looking like this:
<div class="form-group">
<label>Firm:</label>
<select class="form-control marg-left-10" id="firm">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="form-group">
<label>Administrator</label>
<select class="form-control marg-left-10" id="admin"></select>
</div>
<script>
$("#firm").change(function() {
var selectedMark = $("#firm option:selected").val();
var selectedMarkt = $("#firm option:selected").text();
if (selectedMark != "") {
$.ajax({
type: "GET",
url: "test.php",
success: function(data) {
$("#admin").append("<option value='" + selectedMark + "'>" + selectedMarkt + "</option>");
}
});
}
});
</script>
Looks like you need another "/" on this line:
url: "<?php echo base_url() . 'user/getAdministrator'; ?>" + selectedMark,
Right after 'user/getAdministrator'.
I am trying to create 3 drop down boxes dependent on each other. Each drop down box is getting its data from its own tables. There are 3 tables as shown:
This is the form:
<label for="tourtype">
Tour Type
</label>
<select id="tourtype" name="tourtype" required>
<option value="" selected="selected">
--Select--
</option>
<?php
$sql=mysql_query("Select tour_type_id,tour_name from tour_type");
while($row=mysql_fetch_array($sql))
{
$tour_type_id=$row['tour_type_id'];
$name=$row['tour_name'];
echo "<option value='$tour_type_id'>$name</option>";
}
?>
</select>
<label>
Country
</label>
<select id="country" name="country" class="country" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
<
<label>
Destination
</label>
<select id="destination" name="destination" class="destination" required>
<option value="" selected="selected">
-- Select --
</option>
</select>
This is the javascript:
<script type="text/javascript">
$('#tour_type').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_countries=1",
success: function (html) {
$("#country").html(html);
}
});
});
$('#country').change(function () {
var id = $(this).val();
$.ajax({
type: "POST",
url: "ajax.php",
data: "&id=" + id + "&get_destination=1",
success: function (html) {
$("#destination").html(html);
}
});
});
</script>
And this is the ajax.php
<?php
include ('../config.php');
< ? php
if ($_REQUEST['get_countries']) {
$sql = mysql_query("SELECT * FROM `countries` where `tour_type_id`=" . $_REQUEST['id']);
$countries = "";
while ($row = mysql_fetch_array($sql)) {
$cid = $row['countries_id'];
$name = $row['countries_name'];
$countries.= "<option value='" . $cid . "'>" . $name . "</option>";
}
echo $countries;
}
elseif ($_REQUEST['get_destination']) {
$destination = "";
$sql = mysql_query("SELECT * FROM `destination` where `country_id` =" . $_REQUEST['id'])
while ($row = mysql_fetch_array($sql)) {
$destination_id = $row['destination_id'];
$name = $row['destination_name'];
$destination.= "<option value='" . $destination_id . "'>" . $name . "</option>";
}
echo $destination;
}
?>
The problem is the 2nd and 3rd drop down boxes are not populating anything. Can anyone help me? For example if i select culture on the 1st drop down, the 2nd drop down should show Holland and Belgium. Then if i select Holland, the 3rd drop down should show Amsterdam.
Your identifier in your Javascript is #tour_type when your id is #tourtype.
If the syntax is correct and your SQL results are correct too, it should work.
EDIT: some of your JS isn't right.
data: "&id=" + id + "&get_countries=1",
should be
data: {id: id, get_countries: 1},
You should also put a debug on your ajax call by adding
error: function () { alert("ajax failed"); }
after your success callback
full sources now:
$('#tourtype').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url:"jurassicbase5/admin/ajax.php",
data: {id: id, get_countries: 1},
success: function(html){
$("#country").empty();
$("#country").append(html);
},
error: function () { alert("ajax failed"); }
});
});
$('#country').change(function() {
var id=$(this).val();
$.ajax
({
type: "POST",
url: "jurassicbase5/admin/ajax.php",
data: {id: id, get_destination: 1},
success: function(html)
{
$("#destination").empty();
$("#destination").append(html);
},
error: function () { alert("ajax failed"); }
});
});