Home » vue.js Dynamic Dependent Dropdown using PHP

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

Read Also:  Laravel 6 Install Constant Contact - PHP Examples

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.

Read Also:  Dropdown Validation using Angular Example

<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 #######.

Read Also:  jQuery ajax pagination in php MySQLi

<?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 :

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.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 5.6 PDF Generation using Dompdf Example
  2. Read Also:  php calculate date difference in laravel carbon query
  3. Read Also:  Ajax Dynamic Dependent Dropdown using Laravel Example
  4. Read Also:  laravel soft delete relationship Eloquent ORM
  5. Read Also:  pl sql queries for practice(SQL Exercises, Practice, Solution)
  6. Read Also:  Laravel 5.8 Global Blade view variable
  7. Read Also:  Image Uploads with Angular 9 Tutorial
  8. Read Also:  Laravel Dependent Dropdown Example Tutorial From Scratch
  9. Read Also:  Vuejs Dropdown Component with example
  10. Read Also:  jquery ajax dropdown onchange example in php MySQL Database

Leave a Comment

Your email address will not be published. Required fields are marked *