Skip to main content

PHP - Submit Post Data Using PDO & AngularJS

Language


In this tutorial we will create a Submit Post Data using PDO & AngularJS. AngularJS is a structural framework for dynamic web apps. PDO stands for PHP Data Objects. It is a lean and consistent way to access databases. This means developers can write portable code much easier. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Lastly, this is the link for the AngularJS https://angularjs.org/.

Creating Database

Open your database web server then create a database name in it db_angular, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

  1. <?php
  2. $conn = new PDO('mysql:host=localhost;dbname=db_angular', 'root', '');
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into you text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html lang = "en">
  3. <head>
  4. <meta charset = "UTF-8" name = "viewport" content = "width=device-width, initial-scale=1"/>
  5. <link rel = "stylesheet" type = "text/css" href = "css/bootstrap.css" />
  6. </head>
  7. <body ng-app = "myModule" ng-controller = "myController">
  8. <nav class = "navbar navbar-default">
  9. <div class = "container-fluid">
  10. <a class = "navbar-brand" href = "https://sourcecodester.com" >Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class = "col-md-3"></div>
  14. <div class = "col-md-6 well">
  15. <h3 class = "text-primary">PHP - Submit Post Data Using PDO & AngularJS</h3>
  16. <hr style = "border-top:1px dotted #ccc;">
  17. <form>
  18. <div class = "form-inline">
  19. <input type = "text" class = "form-control" ng-model = "firstname" placeholder="Firstname"/>
  20. <input type = "text" class = "form-control" ng-model = "lastname" placeholder="Lastname"/>
  21. <input type = "text" class = "form-control" ng-model = "address" placeholder="Address"/>
  22. <br /><br />
  23. <center><button type = "button" class = "btn btn-primary" ng-click = "saveData()" ><span class = "glyphicon glyphicon-save"></span> Submit</button></center>
  24. </div>
  25. </form>
  26. <br />
  27. <table class = "table table-bordered">
  28. <thead class="alert-success">
  29. <tr>
  30. <th>Firstname</th>
  31. <th>Lastname</th>
  32. <th>Address</th>
  33. </tr>
  34. </thead>
  35. <tbody style="background-color:#fff;">
  36. <tr ng-repeat = "member in members">
  37. <td></td>
  38. <td></td>
  39. <td></td>
  40. </tr>
  41. <tr ng-repeat = "member in members">
  42. <td>{{member.firstname}}</td>
  43. <td>{{member.lastname}}</td>
  44. <td>{{member.address}}</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>
  49. <script src = "js/angular.js"></script>
  50. <script src = "js/script.js"></script>
  51. </body>
  52. </html>

Creating the PHP Query

This code contains the php query of the application. This code will store the data inputs to the database and display the data in the page after storing. To do this just copy and write these block of codes inside the text editor and save it as shown below.
save.php

  1. <?php
  2. require_once 'conn.php';
  3. $data = json_decode(file_get_contents("php://input"));
  4.  
  5. $firstname = $data->firstname;
  6. $lastname = $data->lastname;
  7. $address = $data->address;
  8.  
  9.  
  10. try{
  11. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  12. $sql = "INSERT INTO `member`(firstname, lastname, address) VALUES ('$firstname', '$lastname', '$address')";
  13. $conn->exec($sql);
  14. }catch(PDOException $e){
  15. echo $e->getMessage();
  16. }
  17.  
  18. $conn = null;
  19. ?>

data.php

  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $data = array();
  5.  
  6. $sql = "SELECT * FROM `member`";
  7. $query = $conn->prepare($sql);
  8. $query->execute();
  9.  
  10. while($fetch = $query->fetch()){
  11. $data[] = $fetch;
  12. }
  13.  
  14. echo json_encode($data);
  15.  
  16. ?>

Creating the Main Function

This code contains the main function of the application. This code will sent a PDO POST request to the database using the angular directives. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js folder.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http, $timeout){
  3. $http.get('data.php').then(function(response){
  4. $scope.members = response.data;
  5. });
  6.  
  7. $scope.saveData = function(){
  8. if($scope.firstname == null || $scope.lastname == null || $scope.address == null){
  9. alert("Please complete the required field");
  10. }else{
  11. $http.post("save.php", {firstname: $scope.firstname, lastname: $scope.lastname, address: $scope.address})
  12. .then(function(){
  13. $scope.firstname = "";
  14. $scope.lastname = "";
  15. $scope.address = "";
  16. $scope.getData();
  17. });
  18. }
  19.  
  20. }
  21.  
  22. $scope.getData = function(){
  23. $http.get('data.php').then(function(response){
  24. $scope.members = response.data;
  25. });
  26. }
  27. });

There you have it we successfully created a Submit Post Data using PDO & AngularJS. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!

 

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.