Solved

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

Posted on 2013-02-02
7
397 Views
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
0
Comment
Question by:thenelson
  • 3
  • 2
  • 2
7 Comments
 
LVL 6

Expert Comment

by:nickinthooz
Comment Utility
To save the data to a text file you can do something like...

<?php
$field1 = $_POST['yourfield1'];
$field2 = $_POST['yourfield2'];
$fp = fopen(”formdata.txt”, “a”);
$savestring = $field1 . “,” . $field2 . “n”;
fwrite($fp, $savestring);
fclose($fp);
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));
fclose($fh);
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0
 
LVL 6

Expert Comment

by:nickinthooz
Comment Utility
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 39

Author Comment

by:thenelson
Comment Utility
Ray_Paseur:
>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.

Concerning:
"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?

nickinthooz,
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.
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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.
http://www.sitepoint.com/books/phpmysql5/

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:
http://www.php.net/manual/en/function.md5.php

PHP has the $_COOKIE array that lets you find what cookies arrived with your request.
http://www.php.net/manual/en/reserved.variables.cookies.php

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.
http://www.php.net/manual/en/function.setcookie.php

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

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.
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Jquery/A_10712-The-Hello-World-Exercise-with-jQuery-and-PHP.html

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
0
 
LVL 39

Author Closing Comment

by:thenelson
Comment Utility
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

763 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now