• We just launched and are currently in beta. Join us as we build and grow the community.

How to Save Data Automatically as Draft using PHP and jQuery

thoxy

Pun Pundit
T Rep
0
0
0
Rep
0
T Vouches
0
0
0
Vouches
0
Posts
142
Likes
171
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
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.
2018-07-21_13_50_50-localhost_127.0.0.1_db_save_phpmyadmin_4.7.0.png

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_

    id`

    int

    (

    11

    )

    NOT

    NULL

    PRIMARY KEY

    AUTO_INCREMENT

    ,
  3. `blog_

    title`

    varchar

    (

    250

    )

    NOT

    NULL

    ,
  4. `blog_

    content`

    text

    DEFAULT

    NULL

    ,
  5. `status`

    varchar

    (

    20

    )

    NOT

    NULL


  6. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    utf8mb4;

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

    (

    !

    $conn

    )

    {
  4. die

    (

    "Error: Failed to connect to database!"

    )

    ;
  5. }
  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 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. if

    (

    $_POST

    [

    'blog_id'

    ]

    ==

    ""

    )

    {
  4. $title

    =

    $_POST

    [

    'title'

    ]

    ;
  5. $content

    =

    $_POST

    [

    'content'

    ]

    ;

  6. $conn

    ->

    query

    (

    "INSERT INTO `blog` VALUE('', '$title

    ', '$content

    ', 'Post')"

    )

    ;
  7. echo

    $conn

    ->

    insert_id

    ;
  8. }

    else

    {
  9. $title

    =

    $_POST

    [

    'title'

    ]

    ;
  10. $content

    =

    $_POST

    [

    'content'

    ]

    ;
  11. $blog_id

    =

    $_POST

    [

    'blog_id'

    ]

    ;
  12. $conn

    ->

    query

    (

    "UPDATE `blog` SET `blog_title` = '$title

    ', `blog_content` = '$content

    ', `status` = 'Post' WHERE `blog_id` = '$blog_id

    '"

    )

    ;
  13. }
  14. ?>

update_blog.php


  1. <?php
  2. require_once

    'conn.php'

    ;

  3. if

    (

    $_POST

    [

    'blog_id'

    ]

    ==

    ""

    )

    {
  4. $title

    =

    $_POST

    [

    'title'

    ]

    ;
  5. $content

    =

    $_POST

    [

    'content'

    ]

    ;

  6. $conn

    ->

    query

    (

    "INSERT INTO `blog` VALUE('', '$title

    ', '$content

    ', 'Draft')"

    )

    ;
  7. echo

    $conn

    ->

    insert_id

    ;
  8. }

    else

    {
  9. $title

    =

    $_POST

    [

    'title'

    ]

    ;
  10. $content

    =

    $_POST

    [

    'content'

    ]

    ;
  11. $blog_id

    =

    $_POST

    [

    'blog_id'

    ]

    ;
  12. $conn

    ->

    query

    (

    "UPDATE `blog` SET `blog_title` = '$title

    ', `blog_content` = '$content

    ' WHERE `blog_id` = '$blog_id

    ' && `status` = 'Draft'"

    )

    ;
  13. }
  14. ?>

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

    (

    title !=

    ""

    &&

    content !=

    ""

    )

    {
  9. $.ajax

    (

    {
  10. url:

    'save_blog.php'

    ,
  11. type:

    'POST'

    ,
  12. data:

    {

    title:

    title,

    content:

    content,

    blog_id:

    blog_id}

    ,
  13. success:

    function

    (

    )

    {
  14. $(

    '#title'

    )

    .val

    (

    ''

    )

    ;
  15. $(

    '#content'

    )

    .val

    (

    ''

    )

    ;
  16. $(

    '#blog_id'

    )

    .val

    (

    ''

    )

    ;
  17. $(

    '#result'

    )

    .html

    (

    "<center>Post Submitted!</center>"

    )
  18. AutoSave(

    )

    ;
  19. }
  20. }

    )

    ;
  21. }
  22. }

    )

    ;

  23. function

    AutoSave(

    )

    {
  24. _autosave =

    setInterval(

    function

    (

    )

    {
  25. var

    title =

    $(

    '#title'

    )

    .val

    (

    )

    ;
  26. var

    content =

    $(

    '#content'

    )

    .val

    (

    )

    ;
  27. var

    blog_id =

    $(

    '#blog_id'

    )

    .val

    (

    )

    ;

  28. if

    (

    title !=

    ""

    &&

    content !=

    ""

    )

    {
  29. $.ajax

    (

    {
  30. url:

    'update_blog.php'

    ,
  31. type:

    'POST'

    ,
  32. data:

    {

    title:

    title,

    content:

    content,

    blog_id:

    blog_id}

    ,
  33. success:

    function

    (

    data)

    {
  34. if

    (

    data !=

    ""

    )

    {
  35. $(

    '#blog_id'

    )

    .val

    (

    data)

    ;
  36. }
  37. }
  38. }

    )

    ;
  39. }
  40. }

    ,

    2000

    )

    ;
  41. }
  42. AutoSave(

    )

    ;

  43. }

    )

    ;

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 :)


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,526

323,535

Top