How to Save Data Automatically as Draft using PHP and jQuery

In this tutorial we will create a Simple Auto Save Data Using jQuery. By using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by newly coders for its user-friendly environment. jQuery is a cross-platform JavaScript library designed to simplify the client-side scripting of HTML.

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/.

Creating Database

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

You can also use the below code to create a table in your newly created database programmatically. Copy and Paste the code in the SQL Tab.

  1. CREATE TABLE `blog` (
  2.   `blog_title` varchar(250) NOT NULL,
  3.   `blog_content` text DEFAULT NULL,
  4.   `status` varchar(20) NOT NULL

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 mysqli('localhost', 'root', '', 'db_save');
  3.        
  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 your text editor, then save it as index.php.

  1. <!DOCTYPE html>
  2. <html>
  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="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">PHP - Simple Auto Save Data Using jQuery</h3>
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class="col-md-3"></div>
  18.                 <div class="col-md-6">
  19.                         <form method="POST">
  20.                                 <div class="form-group">
  21.                                         <label>Title</label>
  22.                                         <input type="text" class="form-control" id="title"/>
  23.                                         <input type="hidden" id="blog_id"/>
  24.                                 </div>
  25.                                 <div class="form-group">
  26.                                         <label>Content</label>
  27.                                         <textarea id="content" class="form-control" style="resize:none; height:200px;"></textarea>
  28.                                 </div>
  29.                                 <div id="result"></div>
  30.                                 <br />
  31.                                 <center><button type="button" id="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Save Post</button></center>
  32.                         </form>
  33.                 </div>
  34.         </div>
  35. </body>
  36. <script src="js/jquery-3.2.1.min.js"></script>
  37. <script src="js/script.js">
  38. </script>
  39. </html>

Creating PHP Query

This code contains the PHP query of the application. This code will update the blog content automatically and will submit the post after completing the post To do that just copy and write this block of codes inside the text editor, then save it as shown below.

save_blog.php
  1. <?php
  2.         require_once 'conn.php';
  3.  
  4.         if($_POST['blog_id'] == ""){
  5.                 $title = $_POST['title'];
  6.                 $content = $_POST['content'];
  7.  
  8.                 $conn->query("INSERT INTO `blog` VALUE('', '$title', '$content', 'Post')");
  9.                 echo $conn->insert_id;
  10.         }else{
  11.                 $title = $_POST['title'];
  12.                 $content = $_POST['content'];
  13.                 $blog_id = $_POST['blog_id'];
  14.                 $conn->query("UPDATE `blog` SET `blog_title` = '$title', `blog_content` = '$content', `status` = 'Post' WHERE `blog_id` = '$blog_id'");
  15.         }
  16. ?>
update_blog.php
  1. <?php
  2.         require_once 'conn.php';
  3.        
  4.         if($_POST['blog_id'] == ""){
  5.                 $title = $_POST['title'];
  6.                 $content = $_POST['content'];
  7.                
  8.                 $conn->query("INSERT INTO `blog` VALUE('', '$title', '$content', 'Draft')");
  9.                 echo $conn->insert_id;
  10.         }else{
  11.                 $title = $_POST['title'];
  12.                 $content = $_POST['content'];
  13.                 $blog_id = $_POST['blog_id'];
  14.                 $conn->query("UPDATE `blog` SET `blog_title` = '$title', `blog_content` = '$content' WHERE `blog_id` = '$blog_id' && `status` = 'Draft'");
  15.         }
  16. ?>

Creating jQuery Script

This is where the main function of the application. This code will update the blog content base on the set interval. To do this just copy and write these block of codes inside the text editor, then save it as script.js.

  1. $(document).ready(function(){
  2.         var _autosave;
  3.         $('#save').on('click', function(){
  4.                 clearInterval(_autosave);
  5.                 var title = $('#title').val();
  6.                 var content = $('#content').val();
  7.                 var blog_id = $('#blog_id').val();
  8.  
  9.                         if(title != "" && content != ""){
  10.                                 $.ajax({
  11.                                 url: 'save_blog.php',
  12.                                 type: 'POST',
  13.                                 data: {title: title, content: content, blog_id: blog_id},
  14.                                 success: function(){
  15.                                         $('#title').val('');
  16.                                         $('#content').val('');
  17.                                         $('#blog_id').val('');
  18.                                         $('#result').html("<center>Post Submitted!</center>")
  19.                                         AutoSave();
  20.                                 }
  21.                         });
  22.                 }
  23.         });
  24.  
  25.         function AutoSave(){
  26.                 _autosave = setInterval(function(){
  27.                         var title = $('#title').val();
  28.                         var content = $('#content').val();
  29.                         var blog_id = $('#blog_id').val();
  30.          
  31.                                 if(title != "" && content != ""){
  32.                                         $.ajax({
  33.                                         url: 'update_blog.php',
  34.                                         type: 'POST',
  35.                                         data: {title: title, content: content, blog_id: blog_id},
  36.                                         success: function(data){
  37.                                                 if(data != ""){
  38.                                                         $('#blog_id').val(data);
  39.                                                 }
  40.                                         }
  41.                                 });
  42.                         }
  43.                 }, 2000);
  44.         }
  45.         AutoSave();
  46.  
  47. });
DEMO

There you have it we successfully created Simple Auto Save Data Using jQuery. 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