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,
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>
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More
View Comments
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);
});
}
});