PHP - Submit Post Data To SQLite Using PDO & jQuery

Submitted by: 
Visitors have accessed this post 280 times.

In this tutorial we will create a Submit Post Data To SQLite Using PDO&jQuery. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. SQLite is an in-process library that implements a self-contained and a transactional SQL database engine. SQLite generally runs faster the more memory you give it. That's why most of the developer use SQLite as a database engine for their application. 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

And this is the link for the jquery that i used in this tutorial

Lastly, this is the link for the bootstrap that i used for the layout design

Installing SQLite Browser
We will now then install the SQLite data viewer, here's the link for the DB Browser for SQLite

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.
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 PDO('sqlite:db/db_member.sqlite3');
  4. $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  6. $query = "CREATE TABLE IF NOT EXISTS member (mem_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, firstname TEXT, lastname TEXT, address TEXT)";
  8. $conn->exec($query);
  10. ?>

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>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="">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" class="text-primary">PHP - Submit Post Data To SQLite Using PDO & jQuery</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <button class="btn btn-primary" data-toggle="modal" data-target="#form_modal"><span class="glyphicon glyphicon-plus"></span> Add Member</button>
  18. <br /><br />
  19. <table class="table table-bordered">
  20. <thead class="alert-info">
  21. <tr>
  22. <th>Firstname</th>
  23. <th>Lastname</th>
  24. <th>Address</th>
  25. </tr>
  26. </thead>
  27. <tbody style="background-color:#fff;" id="result"></tbody>
  28. </table>
  29. </div>
  30. <div class="modal fade" id="form_modal" aria-hidden="true">
  31. <div class="modal-dialog">
  32. <form method="POST">
  33. <div class="modal-content">
  34. <div class="modal-header">
  35. <h3 class="modal-title">Add Member</h3>
  36. </div>
  37. <div class="modal-body">
  38. <div class="col-md-2"></div>
  39. <div class="col-md-8">
  40. <div class="form-group">
  41. <label>Firstname</label>
  42. <input type="text" id="firstname" class="form-control"/>
  43. </div>
  44. <div class="form-group">
  45. <label>Lastname</label>
  46. <input type="text" id="lastname" class="form-control"/>
  47. </div>
  48. <div class="form-group">
  49. <label>Address</label>
  50. <input type="text" id="address" class="form-control"/>
  51. </div>
  52. </div>
  53. </div>
  54. <div style="clear:both;"></div>
  55. <div class="modal-footer">
  56. <button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save</button>
  57. <button id="close" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  58. </div>
  59. </div>
  60. </form>
  61. </div>
  62. </div>
  63. <script src="js/jquery-3.2.1.min.js"></script>
  64. <script src="js/bootstrap.js"></script>
  65. <script src="js/script.js"></script>
  66. </body>
  67. </html>

Creating the PHP Query

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

  1. <?php
  2. require_once 'conn.php';
  4. $firstname = $_POST['firstname'];
  5. $lastname = $_POST['lastname'];
  6. $address = $_POST['address'];
  8. $query = "INSERT INTO member (firstname, lastname, address) VALUES (:firstname, :lastname, :address)";
  10. $stmt = $conn->prepare($query);
  12. $stmt->bindParam(':firstname', $firstname);
  13. $stmt->bindParam(':lastname', $lastname);
  14. $stmt->bindParam(':address', $address);
  16. $stmt->execute();
  18. echo "success";
  20. ?>


  1. <?php
  2. require 'conn.php';
  3. $query = $conn->prepare("SELECT * FROM `member` ORDER BY `lastname` ASC");
  4. $query->execute();
  5. while($fetch = $query->fetch()){
  7. echo'
  8. <tr>
  9. <td>'.$fetch['firstname'].'</td>
  10. <td>'.$fetch['lastname'].'</td>
  11. <td>'.$fetch['address'].'</td>
  12. </tr>
  13. ';
  14. }
  15. ?>

Creating the Main Function

This code contains the main function of the application. This code will sent a POST request to the SQLite database to store the data inputs into it. 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. $(document).ready(function(){
  3. displayData();
  5. $('#save').on('click', function(){
  6. var firstname = $('#firstname').val();
  7. var lastname = $('#lastname').val();
  8. var address = $('#address').val();
  10. if(firstname == "" || lastname == "" || address == ""){
  11. alert("Please complete the required field!");
  12. }else{
  13. $.post('save_member.php', {firstname: firstname, lastname: lastname, address: address}, function(data){
  14. if(data == "success"){
  15. $('#firstname').val('');
  16. $('#lastname').val('');
  17. $('#address').val('');
  18. $("#form_modal #close").click();
  19. displayData();
  20. }
  21. })
  22. }
  23. });
  25. function displayData(){
  26. $.get( "get_data.php", function( data ) {
  27. $( "#result" ).html( data );
  28. });
  29. }
  31. });

There you have it we successfully created a Submit Post Data To SQLite Using PDO&jQuery. 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.