AngularJS Delete Multiple Rows using PHP/MySQLi

Getting Started

I've used CDN for Bootstrap and Angular JS so you need internet connection for them to work.

Creating our Database

First, we're going to create our database and insert sample data that you can use in deleting. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as angular. 3. After creating a database, click the SQL and paste the below codes. See image below for detailed instruction.
  1. CREATE TABLE `members` (
  2.   `memid` int(11) NOT NULL AUTO_INCREMENT,
  3.   `firstname` varchar(30) NOT NULL,
  4.   `lastname` varchar(30) NOT NULL,
  5.   `address` text NOT NULL,
  6. PRIMARY KEY(`memid`)
  1. INSERT INTO `members` (`memid`, `firstname`, `lastname`, `address`) VALUES
  2. (1, 'Neovic', 'Devierte', 'Silay City'),
  3. (2, 'Julyn', 'Divinagracia', 'E.B. Magalona'),
  4. (3, 'Gemalyn', 'Cepe', 'Bohol'),
  5. (4, 'Matet', 'Devierte', 'Silay City'),
  6. (5, 'Tintin', 'Devierte', 'Silay City'),
  7. (6, 'Bien', 'Devierte', 'Cebu City'),
  8. (7, 'Cherry', 'Ambayec', 'Cebu City'),
  9. (8, 'Jubilee', 'Limsiaco', 'Silay City'),
  10. (9, 'Janna ', 'Atienza', 'Talisay City'),
  11. (10, 'Desire', 'Osorio', 'Bacolod City'),
  12. (11, 'Debbie', 'Osorio', 'Talisay City'),
  13. (12, 'Nipoy ', 'Polondaya', 'Victorias City'),
  14. (13, 'Johnedel', 'Balino', 'Cauyan, Negros'),
  15. (14, 'Nereca', 'Tajonera', 'Cauayan, Negros'),
  16. (15, 'Jerome', 'Robles', 'Cebu City');
database sql

index.html

Next, this is our index that contains our table.
  1. <!DOCTYPE html>
  2. <html lang="en" ng-app="app">
  3.     <meta charset="utf-8">
  4.     <title>AngularJS Delete Multiple Rows using PHP/MySQLi</title>
  5.     <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
  6.     <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script>
  7. </head>
  8. <body ng-controller="memberdata" ng-init="fetch()">
  9. <div class="container">
  10.     <h1 class="page-header text-center">AngularJS Delete Multiple Rows using PHP/MySQLi</h1>
  11.     <div class="row">
  12.                 <div class="col-md-8 col-md-offset-2">
  13.                         <div class="alert alert-success text-center" ng-show="success">
  14.                                 <button type="button" class="close" ng-click="clearMessage()"><span aria-hidden="true">&times;</span></button>
  15.                                 <i class="fa fa-check"></i> {{ successMessage }}
  16.                         </div>
  17.                         <div class="alert alert-danger text-center" ng-show="error">
  18.                                 <button type="button" class="close" ng-click="clearMessage()"><span aria-hidden="true">&times;</span></button>
  19.                                 <i class="fa fa-warning"></i> {{ errorMessage }}
  20.                         </div>
  21.                         <button class="btn btn-danger" ng-click="deleteAll()"><span class="glyphicon glyphicon-trash"></span> Delete</button>
  22.                         <table class="table table-bordered table-striped" style="margin-top:10px;">
  23.                                 <thead>
  24.                     <tr>
  25.                         <th><input type="checkbox" ng-model="checkAll" ng-change="toggleAll()"></th>
  26.                         <th>First Name</th>
  27.                         <th>Last Name</th>
  28.                         <th>Address</th>
  29.                    </tr>
  30.                 </thead>
  31.                                 <tbody>
  32.                                         <tr ng-repeat="member in members">
  33.                                                 <td><input type="checkbox" ng-model="member.Selected" ng-change="toggleOne()"></td>
  34.                                                 <td>{{ member.firstname }}</td>
  35.                                                 <td>{{ member.lastname }}</td>
  36.                                                 <td>{{ member.address }}</td>
  37.                                         </tr>
  38.                                 </tbody>
  39.                         </table>
  40.                         </div>
  41.                 </div>
  42.         </div>
  43. </div>
  44. <script src="angular.js"></script>
  45. </body>
  46. </html>

angular.js

This contains our angular.js script.
  1. var app = angular.module('app', []);
  2. app.controller('memberdata',function($scope, $http){
  3.     $scope.success = false;
  4.     $scope.error = false;
  5.  
  6.     $scope.fetch = function(){
  7.         $http.get("fetch.php").success(function(data){
  8.                 $scope.members = data;
  9.             });
  10.     }
  11.  
  12.     $scope.toggleAll = function(){
  13.         for (var i = 0; i < $scope.members.length; i++) {
  14.             $scope.members[i].Selected = $scope.checkAll;
  15.         }
  16.     };
  17.  
  18.     $scope.toggleOne = function(){
  19.         $scope.checkAll = true;
  20.         for (var i = 0; i < $scope.members.length; i++) {
  21.             if (!$scope.members[i].Selected) {
  22.                 $scope.checkAll = false;
  23.                 break;
  24.             }
  25.         };
  26.     };
  27.        
  28.     $scope.deleteAll = function(){
  29.         checkedId = [];
  30.         for (var i = 0; i < $scope.members.length; i++) {
  31.             if ($scope.members[i].Selected) {
  32.                 checkedId.push($scope.members[i].memid);
  33.             }
  34.         }
  35.         $http.post("delete.php", checkedId)
  36.         .success(function(data){
  37.             console.log(data);
  38.             if(data.error){
  39.                 $scope.error = true;
  40.                 $scope.success = false;
  41.                 $scope.errorMessage = data.message;
  42.                 $scope.checkAll = false;
  43.             }
  44.             else{
  45.                 $scope.success = true;
  46.                 $scope.error = false;
  47.                 $scope.successMessage = data.message;
  48.                 $scope.fetch();
  49.                 $scope.checkAll = false;
  50.             }
  51.         });
  52.     }
  53.  
  54.     $scope.clearMessage = function(){
  55.         $scope.success = false;
  56.         $scope.error = false;
  57.     }
  58.  
  59. });

fetch.php

This is our PHP api/code that fetches table data from our MySQL Database.
  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'angular');
  3.        
  4.         $output = array();
  5.         $sql = "SELECT * FROM members";
  6.         $query=$conn->query($sql);
  7.         while($row=$query->fetch_array()){
  8.                 $output[] = $row;
  9.         }
  10.  
  11.         echo json_encode($output);
  12. ?>

delete.php

Lastly, this is our PHP code/api in deleting the checked rows of our table.
  1. <?php
  2.   $conn = new mysqli('localhost', 'root', '', 'angular');
  3.  
  4.   $data = json_decode(file_get_contents("php://input"));
  5.  
  6.   $out = array('error' => false);
  7.  
  8.   foreach ($data as $key => $value) {
  9.     $sql = "DELETE FROM members WHERE memid = '".$value."'";
  10.     $query = $conn->query($sql);
  11.   }
  12.  
  13.   if($query){
  14.     $out['message'] = "Member(s) deleted Successfully";
  15.   }
  16.   else{
  17.     $out['error'] = true;
  18.     $out['message'] = "Something went wrong. Cannot delete Member(s)";
  19.   }
  20.  
  21.   echo json_encode($out);
  22. ?>
That ends this tutorial. Happy Coding :)

Add new comment