In This simple PHP tutorial, I will let you know how to fill dropdown with non ajax manner. Normally we are refreshing dropdown based on selected master dropdown value, i.e we are using country and states two dropdown and based on country selected we will fill state dropdown. There are two method one is – we can fire a Ajax request based on selected country and other is we can set state using json object without ajax round-trip. This can improve your website response time. Here we will learn that.
Also Checkout other Dropdown jQuery tutorial,
- How to Add Search Functionality in Dropdown List Using jQuery
- How to add Custom message into Select2
- How to Select All element of SELECT2
Below are simple steps to filled dropdown values with non ajax method:
Steps 1: We will get all necessary data from both dropdown based on our requirement.
Steps 2: We will convert that array into json object.
<script> var _stateData = {}; <?php if(!empty($states)):?> var _stateData = <?php echo json_encode($states); ?>; <?php endif; ?> </script>
Steps 3: We will create HTML Layout.
<div class="col-sm-4 row"> <select class="form-control country-select" target="#states_ddl"> <option value="">-- Select Country --</option> <?php foreach($country_list as $k => $v): ?> <option value="<?php echo $k; ?>"><?php echo $v; ?></option> <?php endforeach; ?> </select> </div> <div class="col-sm-4"> <select class="form-control" id="states_ddl"> <option value="">-- Select State --</option> </select> </div>
Steps 4: We will iterate on json object and append to state dropdown list.
<script> jQuery(document).ready(function() { var doc = $(document); doc.delegate('.country-select', 'change', function(e){ var source = $(this), val = $.trim(source.val()), target = source.attr('target'); $(target).empty(); if(typeof(_stateData[val]) != "undefined"){ var options = (typeof(_stateData[val]) != "undefined") ? _stateData[val] : {}; $('<option>-- Select State --</option>').appendTo(target); $.each( options , function(value, index) { $('<option value="' + value + '">' + index + '</option>').appendTo(target); }); } }); }); </script>
thank you very much for this post:D,, but can you please one more field “CITY” with drop down in above post??
Help me please..Thanks in Advance!!!
Thanks Gagan,I will add one more dropdown and update you.
Hi Ganagn,
This is solution as per your requirement.
define php array:
$city = array(
‘DL’ => array(‘SD’ => ‘South DElhi’,
‘ND’ => ‘North Delhi’),
‘UP’ => array(‘LKO’ => ‘Lucknow’,
‘NO’ => ‘NOIDA’)
);
Assigned data to json object:
var _cityData = {};
var _cityData = ;
html code:
— Select City —
jquery code:
doc.delegate(‘.state-select’, ‘change’, function(e){
var source = $(this),
val = $.trim(source.val()),
target = source.attr(‘target’);
$(target).empty();
if(typeof(_cityData[val]) != “undefined”){
var options = (typeof(_cityData[val]) != “undefined”) ? _cityData[val] : {};
$(‘– Select City –‘).appendTo(target);
$.each( options , function(value, index) {
$(” + index + ”).appendTo(target);
});
}
});