Go Premium for a chance to win a PS4. Enter to Win


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

Posted on 2013-02-02
Medium Priority
Last Modified: 2013-02-09
I have a long and complicated form (www.barnwellmd.com/patients/IntakeQuestionnaire.htm).  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
Question by:thenelson
  • 3
  • 2
  • 2

Expert Comment

ID: 38846920
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.
LVL 111

Expert Comment

by:Ray Paseur
ID: 38846978
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

Expert Comment

ID: 38847134
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.
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 39

Author Comment

ID: 38847339
>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.
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 38847557
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
LVL 39

Author Closing Comment

ID: 38870478
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.
LVL 111

Expert Comment

by:Ray Paseur
ID: 38870738
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Suggested Courses

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question