Skip to main content

vue.js Dynamic Dependent Dropdown using PHP

Today, We want to share with you vue.js Dynamic Dependent Dropdown using PHP MySQL.In this post we will show you Dynamic Dependent Dropdown with Vue.js and PHP, hear for dynamic dropdown in larave Example with Demo we will give you demo and example for implement.In this post, we will learn about laravel, jquery ajax categories and subcategories, select dropdown with an example.

vue.js Dynamic Dependent Dropdown using PHP MySQL

There are the Following The simple About vue.js Dynamic Dependent Dropdown using PHP MySQL Full Information With Example and source code.

As I will cover this Post with live Working example to develop country state city drop down list in laravel, so the vue.js php dynamic dependant dropdown for this example is following below.

MySQL Table structure

countries MySQL table

CREATE TABLE `countries` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(80) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

list_of_sts MySQL table

Also Read :  Clear All Cache From Laravel project 5.7

CREATE TABLE `list_of_sts` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(80) NOT NULL,
  `country` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

list_of_cities MySQL table

CREATE TABLE `list_of_cities` (
  `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  `name` varchar(80) NOT NULL,
  `list_of_st` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

config.php

<?php
$host = "localhost";
$user = "root";
$password = ""; 
$dbname = "tutorial";

$con = mysqli_connect($host, $user, $password,$dbname);
// Check connection
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}

Step 1: Include Vue.js library

At first we need to include Vue.js Latest library file.1. Download & Include

(https://unpkg.com/axios/dist/axios.min.js).
<script src="vue.js"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

Step 2: JavaScript/Vue.js Source Code:

This file All the Data contains the following JavaScript/Vue.js Source codes.

var project = new Vue({
  el: '#myproject',
  data: {
    country: 0,
    countries: '',
    list_of_st: 0,
    list_of_sts: '',
    city: 0,
    list_of_cities: ''
  },
  methods: {
    getCountry: function(){

      axios.get('ajaxfile.php', {
        params: {
          request: 1
        }
      })
      .then(function (results_data) {
         project.countries = results_data.data;

         // Empty list_of_st and city
         project.list_of_sts = '';
         project.list_of_cities = '';
         project.list_of_st = 0;
         project.city = 0;
      });

    },
    getCountryStates: function(){

      axios.get('ajaxfile.php', {
         params: {
           request: 2,
           country: this.country
         }
      })
      .then(function (results_data) {
         project.list_of_sts = results_data.data;
         project.list_of_st = 0;

         // Empty city
         project.list_of_cities = '';
         project.city = 0;
      }); 
    }, 
    getStateCities: function(){

      axios.get('ajaxfile.php', { 
        params: {
          request: 3,
          list_of_st: this.list_of_st
        }
      }) 
      .then(function (results_data) {
        project.list_of_cities = results_data.data;
        project.city = 0;
      }); 
    }
  },
  created: function(){
    this.getCountry();
  }
})

Step 3: HTML Source Code:

This file contains the following HTML Markup Source codes.

Also Read :  How to get last record from MySQL table in Laravel?

<div id='myapp'>

  <table>
    <tr>
      <td>Country</td>
      <td>
        <select v-model='country' @change='getCountryStates()'>
          <option value='0' >Select Country</option>
          <option v-for='data in countries' :value='data.id'>{{ data.name }}</option>
        </select>
      </td>
    </tr>

    <tr>
      <td>State</td>
      <td>
        <select v-model='list_of_st' @change='getStateCities()'>
          <option value='0' >Select State</option>
          <option v-for='data in list_of_sts' :value='data.id'>{{ data.name }}</option>
        </select>
      </td>
    </tr>

    <tr>
      <td>City</td>
      <td>
        <select v-model='city' >
          <option value='0' >Select City</option>
          <option v-for='data in list_of_cities' :value='data.id'>{{ data.name }}</option>
        </select>
      </td>
    </tr>
  </table>

</div>

Step 4: PHP Source Code

Following PHP Source codes are used for #######.

<?php
include "config.php";

$request = $_GET['request'];

// Fetch Countries
if($request == 1){

  $result = mysqli_query($con,"SELECT * FROM countries");

  $results_data = array();
  while($row = mysqli_fetch_assoc($result)){
    $id = $row['id'];
    $name = $row['name'];

    $results_data[] = array("id"=>$id,"name"=>$name);
  }

  echo json_encode($results_data);
  exit;
}

// Fetch States of a Country
if($request == 2){
  $country = $_GET['country'];
  $result = mysqli_query($con,"SELECT * FROM list_of_sts WHERE country=".$country);

  $results_data = array();
  while($row = mysqli_fetch_assoc($result)){
    $id = $row['id'];
    $name = $row['name'];

    $results_data[] = array("id"=>$id,"name"=>$name);
  }

  echo json_encode($results_data);
  exit;
}

// Fetch Cities of a State
if($request == 3){
  $list_of_st = $_GET['list_of_st'];
  $result = mysqli_query($con,"SELECT * FROM list_of_cities WHERE list_of_st=".$list_of_st);

  $results_data = array();
  while($row = mysqli_fetch_assoc($result)){
    $id = $row['id'];
    $name = $row['name'];

    $results_data[] = array("id"=>$id,"name"=>$name);
  }

  echo json_encode($results_data);
  exit;
}

Angular 6 CRUD Operations Application Tutorials

Read :

Also Read :  Laravel Simple CRUD Application Script

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about vue.js Dynamic Dependent Dropdown using PHP MySQL.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Web Developer

Hi, I’m Web developer, PHP developer.Pakainfo.com is the most popular Programming & Web Development blog. Our mission is to provide the best online resources on programming and web development.Any visitors of this site are free to browse our tutorials, live demos, Examples and download scripts.If you have a project that you want to get started, launch quickly, contact me. Email : [email protected]