How to autosave data after some time using PHP, jQuery, and AJAX

To implement an auto-save feature using PHP, jQuery, and AJAX, you can follow these steps:

Frontend (jQuery and HTML)

Create an HTML form: Build an HTML form where users can input data.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Auto-Save Example</title>
    <script src=""></script>
    <form id="dataForm">
        <textarea id="data" rows="4" cols="50"></textarea>
    <div id="status">Changes not saved</div>
    <script src="main.js"></script>

Create a JavaScript file (main.js): In the JavaScript file, implement the auto-save functionality.

$(document).ready(function() {
    let timeout; // Variable to store the timeout ID
    let isDirty = false; // Track if the form data has changed

    // Function to send an AJAX request to save the data
    function saveData() {
        const data = $('#data').val();

            url: 'save.php',
            type: 'POST',
            data: { data: data },
            success: function(response) {
                if (response === 'success') {
                    $('#status').text('Changes saved');
                    isDirty = false;
                } else {
                    $('#status').text('Error saving changes');

    // Detect changes in the form data
    $('#data').on('input propertychange', function() {
        isDirty = true;
        $('#status').text('Changes not saved');

        // Clear any previous timeout

        // Set a new timeout to auto-save after 5 seconds of inactivity
        timeout = setTimeout(function() {
            if (isDirty) {
        }, 5000); // 5000 milliseconds (5 seconds)

    // Automatically save data when the user leaves the page
    $(window).on('beforeunload', function() {
        if (isDirty) {

Backend (PHP)

Create a PHP file (save.php): This file will handle the AJAX requests and save the data.

// Your database connection or data storage logic should go here

if (isset($_POST['data'])) {
    $data = $_POST['data'];

    // Replace this with your logic to save the data
    // For example, you can save it to a database or a file
    // Simulate a successful save in this example
    $result = 'success';
} else {
    $result = 'error';

echo $result;

In this example, when a user inputs data into the form (#data), the JavaScript code detects changes and sets a timer to auto-save the data after 5 seconds of inactivity. The form data is sent to the save.php script via AJAX, which can then handle saving the data to your preferred storage.

Additionally, the code triggers an auto-save when the user leaves the page to ensure changes are not lost.

Remember to replace the placeholder logic in save.php with your actual data-saving logic, such as saving data to a database or a file.

This basic example demonstrates how to implement auto-saving with PHP, jQuery, and AJAX. You can enhance it by adding error handling and further customization based on your project requirements.

Muhammad Alfaiz
Alfaiz 6 months ago
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x