PHP AngularJS populate dynamic dropdown example
Sometime, we may need to create dynamic dropdown in our angular application using PHP or using API. here i will give you small example for populate dropdown dynamically from database using php mysql. you can simply create country state city drop down list using angular.
Here, we will bind two select box state and city. so here we will create two tables called "demo_state" and "demo_cities". so here i will explain step by step. you need to just create two tables with bellow sql query and then add some dummy records.
So let's follow bellow three step to complete full example.
Step 1: Create Tables
now we need to create two tables "demo_state" and "demo_cities". you can follow both tables mysql query for create tables.
demo_state table query:
CREATE TABLE IF NOT EXISTS `demo_state` (
`id` int(11) NOT NULL,
`name` varchar(155) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
demo_cities table query:
CREATE TABLE IF NOT EXISTS `demo_cities` (
`id` int(11) NOT NULL,
`state_id` int(12) NOT NULL,
`name` varchar(155) NOT NULL,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2: Create index.php file
index.php
<!DOCTYPE html>
<html>
<head>
<title>PHP AngularJS populate dynamic dropdown example - ItSolutionStuff.com</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
</head>
<body>
<div class="container" style="width:500px;">
<h3 align="center">PHP AngularJS populate dynamic dropdown example - ItSolutionStuff.com</h3>
<br />
<div ng-app="myapp" ng-controller="usercontroller" ng-init="loadState()">
<select name="state" ng-model="state" class="form-control" ng-change="loadCity()">
<option value="">Select state</option>
<option ng-repeat="state in states" value="{{state.id}}">{{state.name}}</option>
</select>
<br />
<select name="city" ng-model="city" class="form-control">
<option value="">Select city</option>
<option ng-repeat="city in cities" value="{{city.id}}">
{{city.name}}
</option>
</select>
</div>
</div>
</body>
</html>
<script>
var app = angular.module("myapp",[]);
app.controller("usercontroller", function($scope, $http){
$scope.loadState = function(){
$http.get("load_state.php")
.success(function(data){
$scope.states = data;
})
}
$scope.loadCity = function(){
$http.post("load_city.php", {'state_id':$scope.state})
.success(function(data){
$scope.cities = data;
});
}
});
</script>
Step 3: Create API files
load_state.php
<?php
$connect = mysqli_connect("localhost", "root", "password", "database_name");
$output = [];
$query = "SELECT * FROM demo_state ORDER BY name ASC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$output[] = $row;
}
echo json_encode($output);
?>
load_city.php
<?php
$connect = mysqli_connect("localhost", "root", "password", "database_name");
$output = [];
$data = json_decode(file_get_contents("php://input"));
$query = "SELECT * FROM demo_cities WHERE state_id='".$data->state_id."' ORDER BY name ASC";
$result = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($result))
{
$output[] = $row;
}
echo json_encode($output);
?>
I hope it can help you...

Hardik Savani
I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 5.6 CRUD Application for Starter
- PHP MySQL - Simple Image Gallery CRUD example from scratch
- Codeigniter - Dynamic dependent dropdown using jquery ajax Example
- PHP - How to make dependent dropdown list using jquery Ajax?
- Laravel - Dynamic Dependant Select Box using JQuery Ajax Example - Part 1
- Laravel Dynamic Dependent Dropdown Example
- JQuery - Display loading image on ajax call example