PHP AngularJS populate dynamic dropdown example

PHP AngularJS populate dynamic dropdown example

In this post we will give you information about PHP AngularJS populate dynamic dropdown example. Hear we will give you detail about PHP AngularJS populate dynamic dropdown exampleAnd how to use it also give you demo for it if it is necessary.

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 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" 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" >

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


Also see:JQuery – Display loading image on ajax call example

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

Also see:PHP Laravel 5.6 – Rest API with Passport Tutorial

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

Hope this code and post will helped you for implement PHP AngularJS populate dynamic dropdown example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

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

12  +    =  22

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US