PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   PHP AutoSave Form Draft   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage
Class: PHP AutoSave Form Draft
Save form input values in browser storage as draft
Author: By
Last change:
Date: 9 years ago
Size: 2,211 bytes
 

Contents

Class file image Download
<?PHP
/*
instantiate the class
you could also over-ride the timer interval here with autosave(int timerInterval)
timerInterval is the number of seconds between each auto save, the default value is 60 seconds (1 minute)
*/
include('autosave.class.php');
$auto = new autosave;

/*
add elements to be auto saved
method addElement(string id[, bool sessionOnly])
id is the id of the element to be auto saved
sessionOnly, true = saved data goes away if browser closed or submitted, false [default] saved data remains until submitted
*/
$auto->addElement('persistent');
$auto->addElement('sessiononly',true);

/*
do something if request data submitted
in this case we are just sending ourselves back to the example script so that we can refresh without the resubmit form warning
you will do whatever tasks planned to process the submitted data
*/
if( !empty($_REQUEST) ){
   
header('location: example.php');
}

/*
notes on the markup below...

onsubmit="clearDraft()" must be included in the form tag to clear the auto saved data when the form is submitted, otherwise
the next time the same user accesses this form for a new submission, the auto saved info will have populated the field.

you must specify a unique id for each element to be auto saved and this id must be added using the addElement method above

the generateScript method must be added after the page body, as shown below. This is required so that the auto save elements
are present before the script tries to populate them with the saved information.
*/
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Auto Save Testing</title>
    </head>
    <body>
        <form method="post" onsubmit="clearDraft()">
            This is a persistent save and will be available until submitted, even if the browser is closed<br>
            <textarea name="persistent" id="persistent" style="width:400px;height:120px;"></textarea><br><br>
            This is a session only save and will be available until submitted or the browser is closed<br>
            <textarea name="sessiononly" id="sessiononly" style="width:400px;height:120px;"></textarea><br><br>
            <input type="submit" value="Submit">
        </form>
    </body>
    <?PHP echo $auto->generateScript();?>
</html>