Skip to main content

Submit Post Using AngularJS & SQLite Source Code

Screenshot
Body
In this tutorial we will create a Submit Post Using AngularJS & SQLite. This code will submit the POST data with the use of Angular directive when the user click the submit button. The code use angular directives to insert the form data that enable to send a POST request to the SQLite query and store it to database 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 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/. Installing SQLite Browser We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite http://sqlitebrowser.org/.

Setting Up SQLite

First, we are going to enable SQLite 3 in our PHP.
  1. Open localhost server folder XAMPP, etc and locate php.ini.
  2. Open php.ini and enable sqlite3 by removing the semicolon in the line. tut1
  3. Save changes and Restart Server.

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 SQLite3('db/db_member') or die("Unable to open database!");
  3. $query="CREATE TABLE IF NOT EXISTS `member`(mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT)";
  4.  
  5. $conn->exec($query);
  6. ?>

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="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">Submit Post Using AngularJS & SQLite Source Code</h3>
  16. <hr style="border-top:1px dotted #ccc;">
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-group">
  20. <input type="text" class="form-control" ng-model="firstname" placeholder="Firstname"/>
  21. </div>
  22. <div class="form-group">
  23. <input type="text" class="form-control" ng-model="lastname" placeholder="Lastname"/>
  24. </div>
  25. <div class="form-group">
  26. <input type="text" class="form-control" ng-model="address" placeholder="Address"/>
  27. </div>
  28. <center><button type = "button" class = "btn btn-primary" ng-click = "saveData()" ><span class = "glyphicon glyphicon-save"></span> Submit</button></center>
  29. </form>
  30. </div>
  31. <div class="col-md-8">
  32. <table class = "table table-bordered">
  33. <thead class="alert-info">
  34. <tr>
  35. <th>Firstname</th>
  36. <th>Lastname</th>
  37. <th>Address</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>{{member.address}}</td>
  45. </tr>
  46. </tbody>
  47. </table>
  48. </div>
  49. </div>
  50. <script src="js/angular.js"></script>
  51. <script src="js/script.js"></script>
  52. </body>
  53. </html>

Creating the Main Function

This code contains the main function of the application. This code will insert a POST data with AngularJS directive 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. 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. $query="INSERT INTO `member` (firstname, lastname, address) VALUES('$firstname', '$lastname', '$address')";
  11.  
  12. $conn->exec($query);
  13.  
  14.  
  15. ?>
data.php
  1. <?php
  2. require_once 'conn.php';
  3.  
  4. $data = array();
  5.  
  6. $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
  7. while($fetch=$query->fetchArray()){
  8. $data[] = $fetch;
  9. }
  10.  
  11. echo json_encode($data);
  12.  
  13. ?>
script.js 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 = 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 using AngularJS & SQLite. 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