display html form data in the original form

thenelson
thenelson used Ask the Experts™
on
I created a html form that posts the text/plain data to a php script on my server. The php script saves the data in a file in the standard text/plain format like this:
Lname=Hochberg
Fname=Nelson
MInitial=
Address=82 E Hey Rd
DOI=
City=
State=AZ
...
Once that I have the data, I would like to refill the form fields with the data so that l can print out the form with the data filled in. Anyone have the code (php or Javascript) to do that?

TIA
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
In your php action script, set the data into php variables:

$lname=$_POST["Lname"];   (or whatever your POST variable names are)
etc.

Then you can recreate the $_POST[] array by

$_POST["Lname"] = $lname;
etc.

Then include the form script at the end of the php script.  This will bring it back onto the screen.

In the HTML script, you can use the same technique to retrieve the variable values

<?php
$lname=$_POST["Lname"];
?>

Then in the form itself, you can put those values into the form:

<input type="text"  name="Lname" value="<? echo $lname; ?>" >

The first time the HTML program is used, those values will be null, so they won't show until you have set them once.
yodercm,

Thanks for your reply.

This particular form has 70 fields. If I understand your suggestion correctly, I would need to write
$lname=$_POST["Lname"];  
$_POST["Lname"] = $lname;
for each field or 70 times. I will be creating a form after this one with about 150 fields.

I am hoping for a solution that loops through each data entry and populates the fields.

The user will fill out the form. Then at a later time, I run the script to repopulate the form and print it out. So the coding should not be in the upload script.

Commented:
Well, you could do that if you give names to all the form inputs such as  input001, input002, etc, and then construct the variables accordingly.
See if something like this helps - (code here).
Proculopsis,

That is closer to what I am looking for. Your solution is similar to:
http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Form-Data.html#Section3

I am looking to repopulate this form:
http://www.barnwellmd.com/NewPt/PatientProfile
(Please do not comment on the messy source code. It was created using MS Word and kompozer which was quick and easy and it works.)
I have attached a data file created by the form.

Thanks!
IntakeQuestionaire-2012-04-16-08.txt

Commented:
Honestly, I don't think there is a trivial way to do this.  You will just have to take the time to do it.  Wish I could give you a quick and easy solution, sorry.
If you are posting the form on same page then you can use below code.

<?php
      // Condition to check if form has been submitted ot nor
      if( isset($_POST['lname']) )
      {
            $formValues      =      extract($_POST);
      }
      else
      {
            $formValues      =      array();
      }
?>

<input type="text" name="lname" id="lname" value="<?php echo $lname;?>" />
There is one more solution.

When you are saving the data in file like this:

Lname=Hochberg
Fname=Nelson
MInitial=
Address=82 E Hey Rd
DOI=
City=
State=AZ

Also store the data in session like:
$_SESSION['Lname']      =      $Lname;

session name or index value must be same as the field name in form.

And while displaying the form, extract the session data like I did in above example for POST array.

Let me know if you need more details.
Try something like this.
that does it. thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial