Skip to main content

Submitting POST Data Without Refreshing/Reloading the Page In PHP and SQLite3 Tutorial


In this tutorial we will create a Submit POST Without Page Refresh In SQLite3 using PHP. This code will submit POST data to the SQLite database without page refresh when the user submits the input fields. The code use PHP POST method to launch a specific function that store the post data to SQLite database without page refresh by sending an ajax request in order to call INSERT SQLite3 query. This is a free program, you can apply this to your system as your own.

We will be using SQLite that 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.

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

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. 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)";
  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 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">PHP - Submit POST Without Page Refresh In SQLite3</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form method="POST">
  19. <div class="form-group">
  20. <label>Firstname</label>
  21. <input type="text" class="form-control" id="firstname"/>
  22. </div>
  23. <div class="form-group">
  24. <label>Lastname</label>
  25. <input type="text" class="form-control" id="lastname"/>
  26. </div>
  27. <div class="form-group">
  28. <label>Address</label>
  29. <input type="text" class="form-control" id="address"/>
  30. </div>
  31. <center><button type="button" class="btn btn-primary" id="save" >Insert</button></center>
  32. </form>
  33. </div>
  34. <div class="col-md-8">
  35. <table class="table table-bordered">
  36. <thead class="alert-info">
  37. <tr >
  38. <th>Firstname</th>
  39. <th>Lastname</th>
  40. <th>Address</th>
  41. </tr>
  42. </thead>
  43. <tbody id="data"></tbody>
  44. </table>
  45. </div>
  46. </div>
  47. <script src="js/jquery-3.2.1.min.js"></script>
  48. <script src="js/bootstrap.js"/></script>
  49. <script src="js/script.js"/></script>
  50. </body>
  51. </html>

Creating the Main Function

This code contains the main function of the application. This code will store the data inputs without page refresh to SQLite database. To do this just copy and write these block of codes as shown below inside the text editor, then save it as shown below.insert.php

  1. <?php
  2. require_once'conn.php';
  3. $firstname=$_POST['firstname'];
  4. $lastname=$_POST['lastname'];
  5. $address=$_POST['address'];
  8. $query="INSERT INTO `member` (firstname, lastname, address) VALUES('$firstname', '$lastname', '$address')";
  10. $conn->exec($query);
  12. echo "Save Data";
  13. ?>
  1. <?php
  2. require'conn.php';
  4. $query=$conn->query("SELECT * FROM `member`") or die("Failed to fetch row!");
  5. while($fetch=$query->fetchArray()){
  6. echo"<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['address']."</td></tr>";
  7. }
  8. ?>
script.js Note: To make the script works make sure you save this file inside the js directory.
  1. $(document).ready(function(){
  2. displayData();
  4. $('#save').on('click', function(){
  5. var firstname = $('#firstname').val();
  6. var lastname = $('#lastname').val();
  7. var address = $('#address').val();
  9. if($('#firstname').val() == "" || $('#lastname').val() == "" || $('address').val()){
  10. alert("Please complete the required field");
  11. }else{
  12. $.ajax({
  13. type: 'POST',
  14. url: 'insert.php',
  15. data: {
  16. firstname: firstname,
  17. lastname: lastname,
  18. address: address
  19. },
  20. success: function(data){
  21. $('#firstname').val('');
  22. $('#lastname').val('');
  23. $('#address').val('');
  24. alert(data);
  25. displayData();
  26. }
  27. })
  28. }
  29. });
  31. function displayData(){
  32. $.ajax({
  33. type: 'POST',
  34. url: 'data.php',
  35. data: {res: 1},
  36. success: function(data){
  37. $('#data').html(data)
  38. }
  39. });
  40. }
  41. });


There you have it we successfully created a Submit POST Without Page Refresh In SQLite3 using PHP. I hope that this simple tutorial helps you to what you are looking for. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Add new comment