Categories: JqueryPhp

How to Fill Dropdown with Non Ajax

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:

Related Post

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>

Live Demo and Download Source Code

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!!!

    • 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);

      });

      }

      });

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

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

2 months ago

What is the Purpose of php_eol in PHP?

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

8 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

8 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

9 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

9 months ago

Categories