Currently I'm working on Large HTML form (up to 100 textboxes/selects/inputs). I want to add feature allowing user to save his progress, to load it in the future. I was thinking about seting cookie and saving form content to the file, but restoring it by inserting in every input value=" is not very clear (and I can't do this for selects!). How can I make easy saving and restoring progress from form?
When user leaves the page, store the form's values in a file with the user's cookie name such that each line from the file corresponds to a field entry type and the value like: input : myusername
. When user comes back and a file with user's cookiename exists, fgets()
the cookie file contents like this:
<?php
$handle = @fopen("/cokies/yourcookiefilename", "r");
if ($handle)
{
while (!feof($handle))
{
$line_data = explode( ":" , fgets($handle) );
switch( trim( array_shift( $line_data ) ) )
{
case "input":
echo "<input type='text' value='" . implode( "" , $line_data ) . "'/>";//user input may contain the : character too, so it is safe to implode()
break;
...
...
...
//case for select and textfield will also be modified accordingly. e.g. if( ! empty( implode(...) ) ) echo " <option selected='selected'>implode(...)</option> ";...
}
}
fclose($handle);
}
?>