Dynamic and Dependent Dropdown list using AngularJS with PHP

Dynamic and Dependent Dropdown list using AngularJS with PHP

In this Post We Will Explain About is Dynamic and Dependent Dropdown list using AngularJS with PHP With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to HTML Drop Down using Chosen and AngularJS Example

In this post we will show you Best way to implement Dynamic Dropdown List Using Angularjs With Php, hear for AngularJS Multiselect Dropdown List using PHP & MySQL with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Dynamic Dropdown list using AngularJS with PHP

In this Example,First of all Add or Include External Libs Like as a(jQuery, css etc..), and then create a simple index.php or index.html page.After that crate a simple javascript file like as a index.js or main.js, It is also add your web-application First Header Part to some priority set.After that Include your relevant CSS Class.

Also Read :  How to get last inserted id in laravel 5.8?

Database

 --  
 -- Table structure for table `teacher`  
 --  
 CREATE TABLE IF NOT EXISTS `teacher` (  
  `teach_id` int(11) NOT NULL AUTO_INCREMENT,  
  `teacher_name` varchar(255) NOT NULL,  
  PRIMARY KEY (`teach_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;  
 --  
 -- Dumping data for table `teacher`  
 --  
 INSERT INTO `teacher` (`teach_id`, `teacher_name`) VALUES  
 (1, 'dr.parag shukla'),  
 (2, 'mayur dhameliya'),  
 (3, 'kalpesh popat');  
 -- --------------------------------------------------------  
 --  
 -- Table structure for table `student`  
 --  
 CREATE TABLE IF NOT EXISTS `student` (  
  `student_id` int(11) NOT NULL AUTO_INCREMENT,  
  `teach_id` int(11) NOT NULL,  
  `student_name` varchar(255) NOT NULL,  
  PRIMARY KEY (`student_id`)  
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;  
 --  
 -- Dumping data for table `student`  
 --  
 INSERT INTO `student` (`student_id`, `teach_id`, `student_name`) VALUES  
 (1, 1, 'jaydeep Gondaliya'),  
 (2, 1, 'Ankit kathiriya'),  
 (3, 1, 'krunal sisodiya'),  
 (4, 2, 'hardik sisodiya'),  
 (5, 2, 'dharmesh chavada'),  
 (6, 2, 'jagdish donga'),  
 (7, 3, 'dinesh soya'),  
 (8, 3, 'vadi sir'),  
 (9, 3, 'parag shukla');  

Include External Libs

<head>  
<title>Live24u Tutorial | Dynamic simple Dropdown list in AngularJS using PHP</title>  
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>  
<!-- Devloped by Pakainfo.com free download examples -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
</head> 

index.php

<!DOCTYPE html>  
 <html>  
 <!-- Devloped by Pakainfo.com free download examples -->
      <body>  
           <br /><br />  
           <div class="container" style="width:800px;">  
                <h3 align="center">Simple Dynamic Dropdown list using AngularJS in PHP</h3>  
                <br />  
                <div ng-app="liveApp" ng-controller="studentCtrl" ng-init="loadTeacher()">  
                     <select name="teacher" ng-model="teacher" class="form-control" ng-change="loadStudent()">  
                          <option value="">Select Teacher</option>  
                          <option ng-repeat="teacher in teachers" value="{{teacher.teach_id}}">{{teacher.teacher_name}}</option>  
                     </select>  
                     <hr />  
                     <select name="student" ng-model="student" class="form-control">  
                          <option value="">Select Studet</option>  
                          <option ng-repeat="student in students" value="{{student.student_id}}">  
                               {{student.student_name}}  
                          </option>  
                     </select>  
                </div>  
           </div>  
      </body>  
 </html>  
 <script>  
 var app = angular.module("liveApp",[]);  
 app.controller("studentCtrl", function($scope, $http){  
      $scope.loadTeacher = function(){  
           $http.get("load_teacher.php")  
           .success(function(data){  
                $scope.teachers = data;  
           })  
      }  
      $scope.loadStudent = function(){  
           $http.post("load_student.php", {'teach_id':$scope.teacher})  
           .success(function(data){  
                $scope.students = data;  
           });  
      }  
 });  
 </script>  

load_teacher.php

<?php  
 //load_teacher.php  
 $connect = mysqli_connect("localhost", "yourpassword", "", "atmiyalive");  
 $response = array();  
 $sql_query = "SELECT * FROM teacher ORDER BY teacher_name ASC";  
 $result = mysqli_query($connect, $sql_query);  
 while($row = mysqli_fetch_array($result))  
 {  
      $response[] = $row;  
 }  
 echo json_encode($response);  
 ?>  

load_student.php

<?php  
 //load_student.php  
 $connect = mysqli_connect("localhost", "username", "yourpassword", "atmiyalive");  
 $response = array();  
 $data = json_decode(file_get_contents("php://input"));  
 $sql_query = "SELECT * FROM student WHERE teach_id='".$data->teach_id."' ORDER BY student_name ASC";  
 $result = mysqli_query($connect, $sql_query);  
 while($row = mysqli_fetch_array($result))  
 {  
      $response[] = $row;  
 }  
 echo json_encode($response);  
 ?>  

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example

Also Read :  Angular 6 - User Registration and Login Example & Tutorial

Example

I hope you have Got What is Dynamic and Dependent Drop Down List using AJAX And how it works.I would Like to have FeedBack From My Blog(Pakainfo.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(Pakainfo.com) Are Most Always Welcome.

Leave a Reply

avatar
  Subscribe  
Notify of