country state city Dynamic dependent dropdown using jquery in codeigniter

country state city Dynamic dependent dropdown using jquery in codeigniter

In this Post We Will Explain About is With Example and Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to dependent drop down list in codeigniter Example

In this post we will show you Best way to implement Dynamic Dependent Select Box using jQuery, Ajax and PHP, hear for country state city drop down list using ajax in codeigniter with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




Codeigniter – Dynamic dependent dropdown using jquery ajax Example

In this Example,First of all Add or Inluce External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priorty set.After that Include your relavant CSS Class.

Phase 1: Download Fresh Codeigniter 3

In First step or init step we will simple step by step download fresh version of PHP based Like as a Codeigniter 3, so if We haven’t download yet simple here then download from here latest version free download: Download Codeigniter 3

Also Read :   Automatically Laravel Create dynamic generate sitemap

Phase 2: Create Database and Configuration

CREATE TABLE `state_rec` (
  `id` int(11) NOT NULL,
  `name` varchar(155) NOT NULL,
  `cr_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `up_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

live_cities table:

CREATE TABLE `live_cities` (
  `id` int(11) NOT NULL,
  `cur_stid` int(12) NOT NULL,
  `name` varchar(155) NOT NULL,
  `cr_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `up_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

config using Codeigniter

application/config/database.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'YOUR_USERNAME',
	'password' => 'YOUR_PASSWORD',
	'database' => 'DATABASE_NAME',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);

Phase 3: Add Route

application/config/routes.php

Also Read :   php - how to get ip address in codeigniter

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$route['default_controller'] = 'welcome';
$route['404_override'] = '';
$route['translate_uri_dashes'] = FALSE;

$route['liveform'] = 'liveController';
$route['liveform/ajax/(:any)'] = 'liveController/liveformAjax/$1';

Phase 4: Create Controller

application/controllers/liveController.php

<?php

class liveController extends CI_Controller {

   /**Devloped by live24u.com free download examples
   */
   public function __construct() { 
      parent::__construct();
      $this->load->database();
   }

   /**Devloped by live24u.com free download examples
   */
   public function index() {
      $states = $this->db->get("state_rec")->result();
      $this->load->view('liveform', array('states' => $states )); 
   } 

   /**Devloped by live24u.com free download examples
   */
   public function liveformAjax($id) { 
       $result = $this->db->where("cur_stid",$id)->get("live_cities")->result();
       echo json_encode($result);
   }

} 

?>

Phase 5: Create View Files

application/views/liveform.phpIn this Last step, You will create a simple on server liveform.php view after that here you will make HTML form with simple input box two dropdown like as a select box. you also bellow source code ajax code here:

<!DOCTYPE html>
<html>
<head>
    <title>Live24u.com - Codeigniter Dependent Dropdown step by step Example with demo</title>
    <script src="plugin/jquery.js"></script>
    <link rel="stylesheet" href="bootstrap-3.min.css">
</head>

<body>
<!-- Devloped by live24u.com free download examples -->
<div class="container">
    <div class="panel panel-primary">
      <div class="panel-heading">Select simple State and get bellow Related City</div>
      <div class="panel-body">
<!-- Devloped by live24u.com free download examples -->
            <div class="form-group">
                <label for="title">Please Select State:</label>
                <select name="state" class="form-control" style="width:350px">
                    <option value="">--- Select State ---</option>
                    <?php
                        foreach ($states as $key => $value) {
                            echo "<option value='".$value->id."'>".$value->name."</option>";
                        }
                    ?>
                </select>
            </div>
<!-- Devloped by live24u.com free download examples -->
            <div class="form-group">
                <label for="title">Select Your City:</label>
                <select name="live_city" class="form-control" style="width:350px">
                </select>
            </div>
<!-- Devloped by live24u.com free download examples -->
      </div>
    </div>
</div>

<script type="text/javascript">

    $(document).ready(function() {
        $('select[name="state"]').on('change', function() {
            var stateID = $(this).val();
            if(stateID) {
                $.ajax({
                    url: '/liveform/ajax/'+stateID,
                    type: "GET",
                    dataType: "json",
                    success:function(data) {
                        $('select[name="live_city"]').empty();
                        $.each(data, function(key, value) {
                            $('select[name="live_city"]').append('<option value="'+ value.id +'">'+ value.name +'</option>');
                        });
                    }
                });
            }else{
                $('select[name="live_city"]').empty();
            }
        });
    });
</script>
<!-- Devloped by live24u.com free download examples -->
</body>
</html>

cmd commands quick run

php -S localhost:8000

Codeigniter rnu in browser

http://localhost:8000/liveform

You are Most welcome in my youtube Channel Please shubscibe my channel. and give me FeedBackMore Details……
Angularjs Example

Also Read :   NodeJS RESTful CRUD API and MYSQL - node js Restify Tutorial





Example

I hope you have Got What is country state city drop down list using ajax in codeigniter And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.