CoderFunda
  • Home
  • About us
    • Contact Us
    • Disclaimer
    • Privacy Policy
    • About us
  • Home
  • Php
  • HTML
  • CSS
  • JavaScript
    • JavaScript
    • Jquery
    • JqueryUI
    • Stock
  • SQL
  • Vue.Js
  • Python
  • Wordpress
  • C++
    • C++
    • C
  • Laravel
    • Laravel
      • Overview
      • Namespaces
      • Middleware
      • Routing
      • Configuration
      • Application Structure
      • Installation
    • Overview
  • DBMS
    • DBMS
      • PL/SQL
      • SQLite
      • MongoDB
      • Cassandra
      • MySQL
      • Oracle
      • CouchDB
      • Neo4j
      • DB2
      • Quiz
    • Overview
  • Entertainment
    • TV Series Update
    • Movie Review
    • Movie Review
  • More
    • Vue. Js
    • Php Question
    • Php Interview Question
    • Laravel Interview Question
    • SQL Interview Question
    • IAS Interview Question
    • PCS Interview Question
    • Technology
    • Other

16 January, 2023

Laravel Dynamic Dependent Dropdown using VueJS and PHP

 Programing Coderfunda     January 16, 2023     Laravel, Vuejs     No comments   

By and large, Unique Ward Select Box is utilized for auto-populate a dropdown list on Dependant information. At the point when you select one drop-down box esteem it will recover new Dependant information from data set table. for the most part you see for a nation, state and city table. At the point when you select country then the state will fill on select box choice of the chose country. Same with respect to city.


As we probably are aware this undertaking can perform utilizing jquery ajax, without jquery ajax we can't perform in light of the fact that we need to refresh select box esteem on in view of progress occasion.


Along these lines, in this post we will make Dynamic Ward Dropdown utilizing vue js and php mysql. for ajax demand we will utilize axios js. it is great work with vuejs. fundamentally, we will make "nations", "states" and "urban areas" table. Then, at that point, we will make three dropdown nation, state and city. After that we will compose php code for getting information from data set table, so compose sql inquiry and back-end rationale.

 

We need to create just two files as listed below:

1) index.php

2) ajaxpro.php

Ok now just create two file with database with following sql query. Let's just follow three step and you will get very simple and amazing example.

Step 1: Create Database Tables

In this step, we will create countries, states and cities table, so i simply give you sql query for create table. simply run that on your database.

Country Table:

CREATE TABLE IF NOT EXISTS `countries` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`name` varchar(80) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

State Table:

CREATE TABLE IF NOT EXISTS `states` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`country_id` int(11) NOT NULL,

`name` varchar(80) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

City Table:

CREATE TABLE IF NOT EXISTS `cities` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`state_id` int(11) NOT NULL,

`name` varchar(80) NOT NULL,

PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;

Step 2: index.php

Now in this step, we will create index.php file with three select box for country, state and city. Then we will write code for vue.js, so simple get code:

<html lang="en">

<head>

<title>Dynamic Dependent Dropdown with Vue JS and PHP - ItSolutionStuff.com</title>

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">

<script src="http://demo.itsolutionstuff.com/plugin/jquery.js"></script>

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

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>

<body>

<div class="container" id="myApp">

<h3>Dynamic Dependent Dropdown with Vue JS and PHP - ItSolutionStuff.com</h3>

<div class="form-group">

<label>Select Country:</label>

<select class='form-control' v-model='country' @change='getStates()'>

<option value='0' >Select Country</option>

<option v-for='data in countries' :value='data.id'>{{ data.name }}</option>

</select>

</div>

<div class="form-group">

<label>Select State:</label>

<select class='form-control' v-model='state' @change='getCities()'>

<option value='0' >Select State</option>

<option v-for='data in states' :value='data.id'>{{ data.name }}</option>

</select>

</div>

<div class="form-group">

<label>Select City:</label>

<select class='form-control' v-model='city' >

<option value='0' >Select City</option>

<option v-for='data in cities' :value='data.id'>{{ data.name }}</option>

</select>

</div>

</div>

<script type="text/javascript">

var app = new Vue({

el: '#myApp',

data: {

country: 0,

countries: '',

state: 0,

states: '',

city: 0,

cities: ''

},

methods: {

getCountries: function(){

axios.get('ajaxpro.php', {

params: {

request: 'country'

}

})

.then(function (response) {

app.countries = response.data;

app.states = '';

app.cities = '';

app.state = 0;

app.city = 0;

});

},

getStates: function(){

axios.get('ajaxpro.php', {

params: {

request: 'state',

country_id: this.country

}

})

.then(function (response) {

app.states = response.data;

app.state = 0;

app.cities = '';

app.city = 0;

});

},

getCities: function(){

axios.get('ajaxpro.php', {

params: {

request: 'city',

state_id: this.state

}

})

.then(function (response) {

app.cities = response.data;

app.city = 0;

});

}

},

created: function(){

this.getCountries();

}

});

</script>

</body>

</html>

Read Also: Laravel 5 and Vue JS CRUD with Pagination example and demo from scratch

Step 3: ajaxpro.php

In this file, we need to create ajaxpro.php and we will configure database details, write code of fetch data from database table. so simple copy below code:

Read Also: Laravel 5.6 - Dynamic Ajax Autocomplete using Vue.js

<?php

$hostName = "localhost";

$username = "root";

$password = "root";

$dbname = "sole";

$mysqli = new mysqli($hostName, $username, $password, $dbname);

switch ($_GET['request']) {

case 'country':

$sql = "SELECT * FROM countries";

break;

case 'state':

$sql = "SELECT * FROM states WHERE country_id = ". $_GET['country_id'];

break;

case 'city':

$sql = "SELECT * FROM cities WHERE state_id = ". $_GET['state_id'];

break;

default:

break;

}

$result = $mysqli->query($sql);

$response = [];

while($row = mysqli_fetch_assoc($result)){

$response[] = array("id"=>$row['id'], "name"=>$row['name']);

}

echo json_encode($response);

?>

Now you can run example and check it your own. I also added demo and you can also download full script.

  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Laravel Vue JS Picture Transfer Model with Demo Hello Folks,In this instructional exercise, I might want to impart to you how to picture transfer utilizing vue js laravel 5.6. We will picture … Read More
  • Laravel Vue JS Axios Post Solicitation Model and Demo Are you new with Laravel Vue and Axios?, In the event that indeed, you are the ideal locations. In this instructional exercise, I will impart to… Read More
  • Laravel Vue JS Limitless Parchment Model with Demo Here, I need to impart to you how to make limitless parchment pagination utilizing vue js and laravel 5.6. we will make bit by bit vuejs boundle… Read More
  • Example of an infinite scroll in Laravel and Vue JS Example of an infinite scroll in Laravel and Vue JSI'll show you how to make infinite scroll pagination with vue js and Laravel 5.6 in this post… Read More
  • Laravel Vue JS PaginationToday, I might want to impart to you how to make laravel vue pagination without any preparation. We will make dynamic pagination with vue.js. We will … Read More
Newer Post Older Post Home

0 comments:

Post a Comment

Thanks

Meta

Popular Posts

  • Spring boot app (error: method getFirst()) failed to run at local machine, but can run on server
    The Spring boot app can run on the online server. Now, we want to replicate the same app at the local machine but the Spring boot jar file f...
  • Log activity in a Laravel app with Spatie/Laravel-Activitylog
      Requirements This package needs PHP 8.1+ and Laravel 9.0 or higher. The latest version of this package needs PHP 8.2+ and Laravel 8 or hig...
  • Laravel auth login with phone or email
          <?php     Laravel auth login with phone or email     <? php     namespace App \ Http \ Controllers \ Auth ;         use ...
  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh
    I had follow these steps to install an configure firebase to my cordova project for cloud messaging. https://medium.com/@felipepucinelli/how...
  • Vue3 :style backgroundImage not working with require
    I'm trying to migrate a Vue 2 project to Vue 3. In Vue 2 I used v-bind style as follow: In Vue 3 this doesn't work... I tried a...

Categories

  • Ajax (26)
  • Bootstrap (30)
  • DBMS (42)
  • HTML (12)
  • HTML5 (45)
  • JavaScript (10)
  • Jquery (34)
  • Jquery UI (2)
  • JqueryUI (32)
  • Laravel (1017)
  • Laravel Tutorials (23)
  • Laravel-Question (6)
  • Magento (9)
  • Magento 2 (95)
  • MariaDB (1)
  • MySql Tutorial (2)
  • PHP-Interview-Questions (3)
  • Php Question (13)
  • Python (36)
  • RDBMS (13)
  • SQL Tutorial (79)
  • Vue.js Tutorial (68)
  • Wordpress (150)
  • Wordpress Theme (3)
  • codeigniter (108)
  • oops (4)
  • php (853)

Social Media Links

  • Follow on Twitter
  • Like on Facebook
  • Subscribe on Youtube
  • Follow on Instagram

Pages

  • Home
  • Contact Us
  • Privacy Policy
  • About us

Blog Archive

  • September (100)
  • August (50)
  • July (56)
  • June (46)
  • May (59)
  • April (50)
  • March (60)
  • February (42)
  • January (53)
  • December (58)
  • November (61)
  • October (39)
  • September (36)
  • August (36)
  • July (34)
  • June (34)
  • May (36)
  • April (29)
  • March (82)
  • February (1)
  • January (8)
  • December (14)
  • November (41)
  • October (13)
  • September (5)
  • August (48)
  • July (9)
  • June (6)
  • May (119)
  • April (259)
  • March (122)
  • February (368)
  • January (33)
  • October (2)
  • July (11)
  • June (29)
  • May (25)
  • April (168)
  • March (93)
  • February (60)
  • January (28)
  • December (195)
  • November (24)
  • October (40)
  • September (55)
  • August (6)
  • July (48)
  • May (2)
  • January (2)
  • July (6)
  • June (6)
  • February (17)
  • January (69)
  • December (122)
  • November (56)
  • October (92)
  • September (76)
  • August (6)

  • Failed to install 'cordova-plugin-firebase': CordovaError: Uh oh - 9/21/2024
  • pyspark XPath Query Returns Lists Omitting Missing Values Instead of Including None - 9/20/2024
  • SQL REPL from within Python/Sqlalchemy/Psychopg2 - 9/20/2024
  • MySql Explain with Tobias Petry - 9/20/2024
  • How to combine information from different devices into one common abstract virtual disk? [closed] - 9/20/2024

Laravel News

  • Validate Controller Requests with the Laravel Data Package - 5/19/2025
  • Deployer - 5/18/2025
  • Transform JSON into Typed Collections with Laravel's AsCollection::of() - 5/18/2025
  • Auto-translate Application Strings with Laratext - 5/16/2025
  • Simplify Factory Associations with Laravel's UseFactory Attribute - 5/13/2025

Copyright © 2025 CoderFunda | Powered by Blogger
Design by Coderfunda | Blogger Theme by Coderfunda | Distributed By Coderfunda