Save a html form to a local drive and reload to the form

I have a long and complicated form (  I would like to create a button that allows the user to save the data that is posted to their hard drive at a location of their choice and another button that reloads the form with the data.  This is so the user can save the data for security in case the post fails and so they can fill out part of the form and come back to complete it later.  It would also allow me to load the data into the form after I receive it so it is easier to read.

How can I do this?  TIA
LVL 39
Who is Participating?
Ray PaseurConnect With a Mentor Commented:
If you use TurboTax, you will find a good design paradigm for a large, multi-page form submission.  They don't do everything right, but the idea of a table of contents (perhaps in a left "navigation" sidebar) is good.  Each link would take you to a separate part of the form.  Each part could be submitted and confirmed independently.

Rather than embark on a long-winded explanation of stuff that will make your eyes glaze over, I'd like to recommend some learning resources that will help you get a grip on the PHP and SQL essentials.  Run, don't walk, to buy this book.  Give it a month or so of concentrated study - do the examples on your own web server and experience the process and the results.

All of the PHP functions are documented in the online man pages.  So to answer the questions about the md5() function, look no further than here:

PHP has the $_COOKIE array that lets you find what cookies arrived with your request.

You can set a cookie on the client browser, and the client will return it upon the next visit.  This is how you might let the client fill out some parts of the form in one sitting and then return later to fill out more of the form.

AJAX, especially via jQuery is a very powerful tool, but it has its own learning curve.  You can learn more here:

Here is my drop-dead simple example of the Hello World in jQuery AJAX.  It demonstrates how to get a communication from the client to the server and back to the client without reloading the entire web page.  The HTTP request takes place in the "background" so the client interaction is much like a desktop application.

HTH, and don't be afraid to consider hiring a professional developer.  Time is money and a pro will save you a lot of both.  If you decide instead to do this yourself, look about Flagstaff for a local PHP user's group (maybe check Meetup or CraigsList) and join up for opportunities to learn and network with other developers.

Best of luck with the project, ~Ray
To save the data to a text file you can do something like...

$field1 = $_POST['yourfield1'];
$field2 = $_POST['yourfield2'];
$fp = fopen(”formdata.txt”, “a”);
$savestring = $field1 . “,” . $field2 . “n”;
fwrite($fp, $savestring);
echo “<h1>form data</h1>”;

Open in new window

Then to read it, you could do something like:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
echo $theData;

Open in new window

Just echo the $theData in to your form.  You'll have to format it to get the results you want but that should basically do it.
Ray PaseurCommented:
I think I might take a different approach by using jQuery.  Here is what I would do...

1. Cookie the browser upon arrival.  The cookie value would be derived by taking an md5() string.  Make the cookie available to JavaScript and/or put the value into the PHP session.

2. Use the cookie as a key to an "anchor" row in the data base.

3. As each form input field loses focus, make an AJAX request to the server sending the contents of the input field.

4. On the server (backend script) update the data base as each input is received.

5. After the form gets the email address and password, you can use these as keys to the other records in the data base.  If you start with the anonymous cookie design you will be doing the same kind of thing that most modern shopping carts do -- remember the client input whether or not they have signed in.

Have you thought about breaking this enormous form into smaller bites?  When you have more than 450 inputs in a form, it may be difficult for the clients to fill it all in at one sitting.  Maybe a concept like "chapters and paragraphs" would help organize the data better?

HTH, ~Ray
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

I completely agree with ray.  If you're going to have personal information in the form, I was hash it out and salt it inside the file, no matter the route you take.  you don't want a text file with peoples information just laying on a computer for all to see.  Use the md5() tag at least.
thenelsonAuthor Commented:
>Have you thought about breaking this enormous form into smaller bites?  
I have thought of this. The form already has 4 different sections and I considered making each section a separate form with a separate submit button. In this case each section would have the user's name and email address so I could match the sections up once I receive them.  My concern is that I find users are easily confused and may be confused by separate submit buttons.  Any thought on this?  I would prefer not separating the four sections to separate pages as the user has three different forms to fill out now (including this one) and this would then become six different forms that need to be filled out.

"1. Cookie the browser upon arrival.  The cookie value would be derived by taking an md5() string.  Make the cookie available to JavaScript and/or put the value into the PHP session.

2. Use the cookie as a key to an "anchor" row in the data base..."
I have never used cookies before nor do I know how to do an AJAX request. Could I see some code on how to do this?

It looks like I would need to write code to save every field. Is there a way to save the entire form with a few lines like the <form enctype="text/plain" method="post"... with the submit button?  

I am guessing "echo $theData;" would just display the text file with the data and not fill the form with the data. Am I correct?  If so, how would I refill the form with the data?

What is a "md5() tag"

Thanks guys for your responses.
thenelsonAuthor Commented:
I decided to break the form up into 4 sections allowing the user to submit each section separately.

I don't use Turbotax so I haven't seen how they do it. Anywhere else I can see that?

Thanks all for your suggestions.
Ray PaseurCommented:
Thanks for the points.  I think you will be happier with the multi-part form.  I'm not able to think of another good example that shows the same design that TurboTax uses.  I'm sure there is one, I just haven't found it.  Maybe some long government or insurance company forms.  You might look around to see if you know an accountant -- their firm can probably show you a quick demo.

Best regards, ~Ray
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.