AngularJS CRUD Operation With PHP/MySQLi - Part 1

In this tutorial we will use AngularJS for CRUD (create, read) Operation with PHP/ MySQLi as a backend development. AngularJs is widely used in developing modern website because of its easy syntax construct and convenient in time developing. It is a library from javascript that compressed and simplified into a whole new level of programming technique. By the way this tutorial is consist of two parts,for we will tackle only about create and select on AngularJS with PHP database. So let's see how its done. Creating the database First, we will create the database, To create a database open the database server such as wamp, xamp, etc. After that go to phpMyadmin then click database, and name your database as "db_angular". Then click SQL and copy and paste the code below and now run the sql command.
  1. CREATE TABLE `member` (
  2. `mem_id` int(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` varchar(30) NOT NULL,
  4. `lastname` varchar(30) NOT NULL
  5. PRIMARY KEY(`mem_id`)
Creating the database connection To create the connect, just simply copy/paste the code that I provided below and save it as "connect.php"
  1. <?php
  2. define('db_host', 'localhost');
  3. define('db_user', 'root');
  4. define('db_pass', '');
  5. define('db_name', 'db_angular');
  6.  
  7. $conn = new mysqli(db_host, db_user, db_pass, db_name);
  8. if(!$conn){
  9. die("Fatal Error: Can't connect to database");
  10. }
  11. ?>
Creating the Mark Up To create the interface of the application, just copy/paste the code below then name it "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 1</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-click = "insertData()"><span class = "glyphicon glyphicon-save"></span> Submit</button>
  26. <br /><br />
  27. <div ng-model = "message" ng-show = "msgBox" class = "alert alert-info">{{message}}</div>
  28. </div>
  29. </form>
  30. <br />
  31. <table class = "table table-responsive table-bordered alert-warning">
  32. <thead>
  33. <tr>
  34. <th>Firstname</th>
  35. <th>Lastname</th>
  36. <th></th>
  37. </tr>
  38. </thead>
  39. <tbody>
  40. <tr ng-repeat = "member in members">
  41. <td>{{member.firstname}}</td>
  42. <td>{{member.lastname}}</td>
  43. <td><center><button class = "btn btn-warning"><span class = "glyphicon glyphicon-edit"></span></button> <button class = "btn btn-danger"><span class = "glyphicon glyphicon-remove"></span></button></center></td>
  44. </tr>
  45. </tbody>
  46. </table>
  47. </div>
  48. </body>
  49. </html>
Coding the Create Function To make the application work, first we will make the php script for the create function, Copy/paste the code below then save it as "insert.php"
  1. <?php
  2. require_once 'connect.php';
  3. $data = json_decode(file_get_contents("php://input"));
  4. $firstname = $data->firstname;
  5. $lastname = $data->lastname;
  6. $conn->query("INSERT INTO `member` (firstname, lastname) VALUES('$firstname', '$lastname')") or die(mysqli_error());
  7. ?>
Coding the Read Function To display the record from the database we will need the php script read function. To do that copy/paste the code I provided below and save it as "select.php"
  1. <?php
  2. require_once 'connect.php';
  3. $query = $conn->query("SELECT * FROM `member`") or die(mysqli_error());
  4. $data = array();
  5.  
  6. while($row = $query->fetch_array()){
  7. $data[] = $row;
  8. }
  9.  
  10. echo json_encode($data);
  11. ?>
Angular JS Directives This is where all the logic comes, and where application work perfectly. Just simply copy/paste the code then name it as "app.js" and save it into the js directory.
  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http, $timeout){
  3.  
  4. $http.get('select.php').then(function(response){
  5. $scope.members = response.data;
  6. });
  7.  
  8. $scope.insertData = function(){
  9. $http.post("insert.php", {firstname: $scope.firstname, lastname: $scope.lastname})
  10. .then(function(){
  11. $scope.message = "Successfully Submit!";
  12. $scope.msgBox = true;
  13. $scope.firstname = "";
  14. $scope.lastname = "";
  15. $timeout(function(){
  16. $scope.msgBox = false;
  17. }, 2000);
  18. $scope.selectData();
  19. });
  20. }
  21.  
  22. $scope.selectData = function(){
  23. $http.get('select.php').then(function(response){
  24. $scope.members = response.data;
  25. });
  26. }
  27. });
There you have it we create the AngularJs CRUD (create, read) Operation simply as that. For the next part of the tutorial we will tackle on how to update and delete the data so just stay tuned to this site. I hope that this simple tutorial help you improve to your programming carrier. Enjoy Coding!!

Tags

Add new comment