PHP - Submit POST Data to MySQLi Using AngularJS

Submitted by: 
Visitors have accessed this post 256 times.

In this tutorial we will create a Submit POST Data to MySQLi Using AngularJS. This code will submit the POST data when the user click the submit button. The code use angular directives to submit the process form data in order to send a POST request to the MySQLi server. This a user-friendly program feel free to modify and use it to your system.

We will be using AngularJS as a framework which has additional custom HTML attributes embedded into it. It can interprets those attributes as directives to bind inputted parts of the page to a model that represent as a standard JavaScript variables.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server

And this is the link for the bootstrap that i used for the layout design

Lastly, this is the link for the AngularJS

Creating Database

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

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=mysqli_connect("localhost", "root", "", "db_submit");
  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 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="<a href=""" rel="nofollow">"</a> >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 to MySQLi Using AngularJS</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" class="form-control" ng-model="firstname"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" class="form-control" ng-model="lastname"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Address</label>
  29. <input type="text" class="form-control" ng-model="address"/>
  30. </div>
  31. <center><button type = "button" class = "btn btn-primary" ng-click = "saveData()" ><span class = "glyphicon glyphicon-save"></span> Save</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <table class = "table table-bordered">
  36. <thead class="alert-success">
  37. <tr>
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Address</th>
  41. </tr>
  42. </thead>
  43. <tbody style="background-color:#fff;">
  44. <tr ng-repeat = "member in members">
  45. <td>{{member.firstname}}</td>
  46. <td>{{member.lastname}}</td>
  47. <td>{{member.address}}</td>
  48. </tr>
  49. </tbody>
  50. </table>
  51. </div>
  52. </div>
  53. <script src = "js/angular.js"></script>
  54. <script src = "js/script.js"></script>
  55. </body>
  56. </html>

Creating the Main Function

This code contains the main function of the application. This code will submit the POST data when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as shown below.

  1. <?php
  2. require_once'conn.php';
  3. $data = json_decode(file_get_contents("php://input"));
  5. $firstname = $data->firstname;
  6. $lastname = $data->lastname;
  7. $address = $data->address;
  9. mysqli_query($conn, "INSERT INTO `member` VALUES('', '$firstname', '$lastname', '$address')") or die(mysqli_error());
  11. ?>


  1. <?php
  2. require_once 'conn.php';
  4. $data = array();
  6. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  8. while($fetch=mysqli_fetch_array($query)){
  9. $data[] = $fetch;
  10. }
  12. echo json_encode($data);
  14. ?>

Note: Make sure you save this file inside the js directory in order the script works.

  1. var app = angular.module("myModule", [])
  2. .controller("myController", function($scope, $http, $timeout){
  3. $http.get('data.php').then(function(response){
  4. $scope.members =;
  5. });
  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. $"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. }
  20. }
  22. $scope.getData = function(){
  23. $http.get('data.php').then(function(response){
  24. $scope.members =;
  25. });
  26. }
  27. });

There you have it we successfully created a Submit POST Data to MySQLi using 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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.