Link to home
Start Free TrialLog in
Avatar of musicmasteria
musicmasteria

asked on

PHP- Saving form info into a txt file to recall later and repopulate the form at a later time?

This question is an extension of this one but going in a different (javascript --> php)
https://www.experts-exchange.com/questions/24425534/Drop-down-List-of-s-determines-of-rows-of-inputs-How.html?cid=239&anchorAnswerId=24455901#a24455901

So that question gets a little crazy because we did so much stuff but basically we created a form and a php preview script that would layout all the info how I wanted it.
(I've attached those files bellow.)

Now we get a little trickier...

4. Saving sessions in .txt files on the server to save what you have so far and come back to it with a session key.

Sesssion key would be the filename and could be like:
"052309-1273.txt"

"052309" being the date they first created the session
and "1273" could be either a random number or a counter, just as long as no two of the same number come up on the same date.

I think it would be best to save the .txt in an xml format to make recalling the info later easier.

Any ideas?
form-test5.html.txt
form-preview.php.txt
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

I did not look at your other question because I do not monitor the JS zone, but it looks like HonorGod did a lot of work for you there.  That is the beauty of EE!

Is this by any chance a homework assignment?  The question in this post sounds academic to the point of being "overly theoretical" and has very little to do with the correct way to handle such a thing in the real world!

First of all, you do not use a session key to save data on the server.  The session expires when the last instance of the browser window is closed (or possibly sooner) and the data is lost.  (Of course, you could put a cookie on the browser to hold this key, but you are exposed to the risk that the client will come back without the cookie, either from another computer or after deleting the cookies, and there are security issues you must deal with when handling cookies).  Instead, this information should be saved in a data base.  The client ID is the unique key to the contents of the data base table.

The best-practices way to go about this is as follows:

1.  Create registration, login and logout scripts so you can know who is using your web site and so you can control access to the data model.  There are any number of books on this topic and most of the frameworks or CMS systems have these capabilities built in.  For details on this andmjuch, much more, buy this book: http://www.sitepoint.com/books/phpmysql1/

2.  Create a data base table with column names that are exact copies of the <input> field names so you have a one-to-one correspondence between each row of the data base and the values in $_POST.

3. When the client logs in, you will find a "client-id" field that matches his login and password.  This field will be a foreign key to the data base table that holds the form information.

4. When the client wants to edit the data, you read the data base table and use the information to prepopulate the form.

5. When the client submits the form, you take the data from $_POST and write it back into the appropriate row of the data base table.

6. Now with the form data recorded, you process the form fields one-at-a-time to see if any corrections are needed, and you use the pre-populated form algorithm to show error messages, if any, and request additional information or updates/corrections.

Best regards, ~Ray
Read this over - it shows how to use PHP to get the variables from the script into the form.  HTH, ~Ray
<?php // RAY_prepopulate_post_example.php
error_reporting(E_ALL);
 
// SET THE DEFAULT VALUES INTO OUR LOCAL VARIABLES - THIS MIGHT COME FROM THE DATA BASE
$fname = 'Joe';
$eaddr = 'Joe@Your.org';
 
// HAS ANYTHING BEEN POSTED?
if (!empty($_POST))
{
// VISUALIZE THE DATA WE RECEIVED
    echo "<br/>POST DATA FOLLOWS: ";
    var_dump($_POST);
 
// COPY POST DATA INTO OUR LOCAL VARIABLES - THIS MIGHT PUT DATA BACK INTO THE DATA BASE
    $fname = $_POST["fname"];
    $eaddr = $_POST["eaddr"];
}
 
// END OF PHP - PUT UP THE PREPOPULATED FORM
?>
<form method="post">
NAME: <input name="fname" value="<?php echo $fname ?>" /><br/>
ADDR: <input name="eaddr" value="<?php echo $eaddr ?>" /><br/>
<input type="submit" />
</form>

Open in new window

Avatar of musicmasteria
musicmasteria

ASKER

Thank you Ray_Paseur for looking into my question.

Firstly, no this is not a homework assignment. I'm just very new and that's all I could think to do.

"The question in this post sounds academic to the point of being "overly theoretical" and has very little to do with the correct way to handle such a thing in the real world!"
Well as I am pretty much teaching myself little by little how to code, I might as well be a student (but there aren't any classes around me). I haven't done anything with Databases yet so I was trying to avoid using them.

"First of all, you do not use a session key to save data on the server. The session expires when the last instance of the browser window is closed (or possibly sooner) and the data is lost."
I'm sorry, I wasn't referring to that kind of a "session key". My kind of "session key" would just be a unique file name for the data to be stored in. Yes, I realize now that a database would be better for storing data rather than a .txt file but that is the part I don't understand how to work with.

" Create registration, login and logout scripts so you can know who is using your web site and so you can control access to the data model."
I'm using a wordpress blog with users already but what I'm trying to do shouldn't necessarily be restricted to people who are logged in.

"Read this over - it shows how to use PHP to get the variables from the script into the form."
I have done that before with another short script I wrote so I know how to do that already but thank you anyway.



OK, good.  Learning PHP without learning about data bases is really "half a loaf" so consider getting this good book that takes both topics together.  It is available in PDF format, too.

http://www.sitepoint.com/books/phpmysql1/

As a general rule, there are things you want to do in every script - start the session, connect to the data base, define constants, etc.  You can roll all these together into a script header and bring it into the top of every script.  Here is a sample of how to do that... (more to follow)

<?php // RAY_config_page_usage.php
 
// LOAD THE CONFIG PAGE FROM THE DOCUMENT ROOT
$config_page = $_SERVER["DOCUMENT_ROOT"] . '/config.php';
@include_once($config_page);
 
// LOAD THE CONFIG PAGE FROM THE CURRENT WORKING DIRECTORY
$config_page = getcwd() . '/config.php';
@include_once($config_page);
 
 
 
// REST OF SCRIPT GOES HERE...

Open in new window

Here is the sort of thing that goes into the top of the "config" scripts... (more to follow)
<?php // config.php
 
// DO NOT RUN THIS SCRIPT STANDALONE
if (count(get_included_files()) < 2) 
{ 
    header("HTTP/1.1 301 Moved Permanently"); 
    header("Location: /"); 
    exit; 
}
 
// SUPPRESS NOTICES IN THIS SCRIPT
error_reporting(E_ALL ^ E_NOTICE);
 
// START THE SESSION
session_start();
 
// ETC...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial