PHP - Send Data To SQLite Using AngularJS

Submitted by: 
Language: 
Visitors have accessed this post 91 times.

In this tutorial we will create a Send Data To SQLite Using AngularJS. PHP is a server-side scripting language designed primarily for web development. SQLite provides an interface for accessing the database. It includes class interfaces to the SQL commands. And also it allows you to create SQL functions and aggregate using PHP. 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 jquery that i used in this tutorial https://jquery.com/.

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

Additionally, you will need to download the AngularJS here's the link 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.  
  3. $conn = new SQLite3('db/db_member.db');
  4.  
  5. $query = "CREATE TABLE IF NOT EXISTS member (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname VARCHAR, lastname VARCHAR, gender VARCHAR, age VARCHAR)";
  6.  
  7. $conn->exec($query);
  8.  
  9. ?>

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" ng-app="myModule">
  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-controller="myController">
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - Send Data To SQLite Using Angular</h3>
  17. <hr style="border-top:1px dotted;"/>
  18. <button class="btn btn-success" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add member</button>
  19. <br /><br />
  20. <table class="table table-bordered">
  21. <thead class="alert-info">
  22. <tr>
  23. <th>Firstname</th>
  24. <th>Lastname</th>
  25. <th>Gender</th>
  26. <th>Age</th>
  27. </tr>
  28. </thead>
  29. <tbody style="background-color:#fff;">
  30. <tr ng-repeat="member in members | orderBy: 'lastname'">
  31. <td>{{member.firstname}}</td>
  32. <td>{{member.lastname}}</td>
  33. <td>{{member.gender}}</td>
  34. <td>{{member.age}}</td>
  35. </tr>
  36. </tbody>
  37. </table>
  38. </div>
  39. <div class="modal fade" id="form_modal" aria-hidden="true">
  40. <div class="modal-dialog">
  41. <div class="modal-content">
  42. <form>
  43. <div class="modal-header">
  44. <h3 class="modal-title">Add Member</h3>
  45. </div>
  46. <div class="modal-body">
  47. <div class="col-md-2"></div>
  48. <div class="col-md-8">
  49. <div class="form-group">
  50. <label>Firstname</label>
  51. <input type="text" ng-model = "firstname" class="form-control"/>
  52. </div>
  53. <div class="form-group">
  54. <label>Lastname</label>
  55. <input type="text" ng-model = "lastname" class="form-control"/>
  56. </div>
  57. <div class="form-group">
  58. <label>Gender</label>
  59. <select class="form-control" ng-model = "gender">
  60. <option value="">--Please select an option--</option>
  61. <option value="Male">Male</option>
  62. <option value="Female">Female</option>
  63. </select>
  64. </div>
  65. <div class="form-group">
  66. <label>Age</label>
  67. <input type="number" min="0" max="200" ng-model = "age" class="form-control"/>
  68. </div>
  69. </div>
  70. </div>
  71. <div style="clear:both;"></div>
  72. <div class="modal-footer">
  73. <button class="btn btn-primary" type="button" ng-click = "saveData()" ><span class="glyphicon glyphicon-save"></span> Save</button>
  74. <button class="btn btn-danger" id="close" type="button" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  75. </div>
  76. </div>
  77. </form>
  78. </div>
  79. </div>
  80. </div>
  81. <script src="js/angular.js"></script>
  82. <script src="js/jquery-3.2.1.min.js"></script>
  83. <script src="js/bootstrap.js"></script>
  84. <script src="js/script.js"></script>
  85. </body>
  86. </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.  
  4. $data = json_decode(file_get_contents("php://input"));
  5.  
  6. $firstname = $data->firstname;
  7. $lastname = $data->lastname;
  8. $age = $data->age;
  9. $gender = $data->gender;
  10.  
  11. $conn->exec("INSERT INTO `member` (firstname, lastname, age, gender) VALUES('$firstname', '$lastname', '$age', '$gender')");
  12. ?>

data.php

  1. <?php
  2. require_once 'conn.php';
  3. $data = array();
  4. $query = $conn->query('SELECT * FROM `member`');
  5. while($fetch = $query->fetchArray()){
  6. $data[] = $fetch;
  7. }
  8.  
  9. echo json_encode($data);
  10. ?>

Creating the Main Function

This code contains the main function of the application. This code will sent a POST request to the SQLite 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.gender == null || $scope.age == null){
  9. alert("Please complete the required field");
  10. }else{
  11. $http.post("save.php", {firstname: $scope.firstname, lastname: $scope.lastname, age: $scope.age, gender: $scope.gender})
  12. .then(function(){
  13. $scope.firstname = "";
  14. $scope.lastname = "";
  15. $scope.age = "";
  16. $scope.gender = "";
  17. $scope.getData();
  18. $(document).ready(function(){
  19. $("#form_modal #close").click();
  20. });
  21.  
  22. });
  23. }
  24.  
  25. }
  26.  
  27. $scope.getData = function(){
  28. $http.get('data.php').then(function(response){
  29. $scope.members = response.data;
  30. });
  31. }
  32. });

There you have it we successfully created a Send Data To SQLite 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.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.