AngularJS CRUD Operation With PHP/MySQLi - Part 2

In this tutorial we will try to do the AngularJS CRUD (Delete, Update) Operation with PHP/MySQLi. In my previous tutorial we already tackle the (create and update) on AngularJS, this time will continue on and try to finish the project. We already created the database and the database connection, we will just add some method to the AngularJS directives and PHP script to make our project more interactive. Before we proceed make sure you already read the previous tutorial and already have the resources for this tutorial, if you don't have try visiting it here AngularJS CRUD Operation With PHP/MySQLi - Part 1 . Recapping the Mark Up In my last tutorial we already created the mark up, we will just add some new script to make the update and delete function worked. So feel free to copy/paste the code below or you can just simply add the new script into your existing index.php.
  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <script src = "js/angular.js"></script>
  5. <script src = "js/app.js"></script>
  6. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  7. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/>
  8. </head>
  9. <body ng-app = "myModule" ng-controller = "myController">
  10. <nav class = "navbar navbar-default">
  11. <div class = "container-fluid">
  12. <a class = "navbar-brand" href = "https://sourcecodester.com" >Sourcecodester</a>
  13. </div>
  14. </nav>
  15. <div class = "col-md-3"></div>
  16. <div class = "col-md-6 well">
  17. <h3 class = "text-primary">AngularJS CRUD OPERATION WITH PHP/MySQLI - Part 2</h3>
  18. <hr style = "border-top:1px dotted #ccc;">
  19. <form>
  20. <div class = "form-inline">
  21. <label>Firstname</label>
  22. <input type = "text" class = "form-control" ng-model = "firstname" id = "firstname"/>
  23. <label>Lastname</label>
  24. <input type = "text" class = "form-control" ng-model = "lastname" id = "lastname"/>
  25. <button type = "button" class = "btn btn-primary form-control" ng-show = "btnInsert" ng-click = "insertData()"><span class = "glyphicon glyphicon-save"></span> Submit</button>
  26. <button type = "button" class = "btn btn-warning form-control" ng-show = "btnUpdate" ng-click = "updateData()"><span class = "glyphicon glyphicon-edit"></span> Update</button>
  27. <br /><br />
  28. <div ng-model = "message" ng-show = "msgBox" class = "{{messageStatus}}">{{message}}</div>
  29. </div>
  30. </form>
  31. <br />
  32. <table class = "table table-responsive table-bordered alert-warning">
  33. <thead>
  34. <tr>
  35. <th>Firstname</th>
  36. <th>Lastname</th>
  37. <th></th>
  38. </tr>
  39. </thead>
  40. <tbody>
  41. <tr ng-repeat = "member in members">
  42. <td>{{member.firstname}}</td>
  43. <td>{{member.lastname}}</td>
  44. <td><center><button type = "button" ng-click = "updateBtn(member.mem_id, member.firstname, member.lastname)" class = "btn btn-warning"><span class = "glyphicon glyphicon-edit"></span></button> <button type = "button" ng-click = "deleteData(member.mem_id);" class = "btn btn-danger"><span class = "glyphicon glyphicon-remove"></span></button></center></td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>
  49. </body>
  50. </html>
Adding New AngularJS Directives In this section we will add some new directives that will make the delete and update works with the mark up language. To do that just copy the code below and paste inside the controller method.
  1. $scope.btnInsert = true;
  2.  
  3. $scope.updateData = function(mem_id){
  4. $scope.btnUpdate = false;
  5. $scope.btnInsert = true;
  6. $http.post("update.php", {mem_id: $scope.mem_id, firstname: $scope.firstname, lastname: $scope.lastname})
  7. .then(function(response){
  8. $scope.firstname = "";
  9. $scope.lastname = "";
  10. $scope.message = "Successfully Updated";
  11. $scope.messageStatus = "alert alert-success";
  12. $scope.msgBox = true;
  13. $timeout(function(){
  14. $scope.msgBox = false;
  15. }, 1000);
  16. $scope.selectData();
  17. });
  18. }
  19.  
  20. $scope.updateBtn = function(mem_id, firstname, lastname){
  21. $scope.btnInsert = false;
  22. $scope.btnUpdate = true;
  23. $scope.firstname = firstname;
  24. $scope.lastname = lastname;
  25. $scope.mem_id = mem_id;
  26. }
Coding the Delete Function This code will delete the data when the user clicked the button within the row. Just copy/paste the code below then save it as 'delete.php'.
  1. <?php
  2. require_once 'connect.php';
  3. $data = json_decode(file_get_contents("php://input"));
  4. $mem_id = $data->mem_id;
  5. $query = $conn->query("DELETE FROM `member` WHERE `mem_id` = $mem_id") or die(mysqli_error());
  6. ?>
Coding the Update Function This code will update the chosen data within the table row when clicked, and simultaneously display the data in the input text box. To make this function works, just simply copy/paste the code below then save it as "update.php"
  1. <?php
  2. require_once 'connect.php';
  3. $data = json_decode(file_get_contents("php://input"));
  4. $mem_id = $data->mem_id;
  5. $firstname = $data->firstname;
  6. $lastname = $data->lastname;
  7. $conn->query("UPDATE `member` SET `firstname` = '$firstname', `lastname` = '$lastname' WHERE `mem_id` = $mem_id") or die(mysqli_error());
  8. ?>
There you have it we successfully completed the project. I hope you learn a lot from this tutorial, and also you can apply this to your on working project. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

Tags

Comments

Submitted bysanjana mulik (not verified)on Thu, 12/28/2017 - 14:13

can u please give deleteData(member.mem_id); function. Its is missing from source code.

Add new comment