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)
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_24425534.html?cid=239#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
LVL 3
musicmasteriaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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
0
Ray PaseurCommented:
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

0
musicmasteriaAuthor Commented:
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.



0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Ray PaseurCommented:
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

0
Ray PaseurCommented:
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

0
Ray PaseurCommented:
Here is the sort of thing you have to do every time you want to use a DB.  That's why I get the variables right once for the four lines starting around line 10, then put this into the config script.

HTH, ~Ray
<?php // RAY_mysql_example.php
error_reporting(E_ALL);
 
// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://us2.php.net/manual/en/ref.mysql.php
// MAN PAGE: http://us2.php.net/manual/en/mysql.installation.php
 
 
// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";
 
// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB CONNECTION: ";
   echo "<br/> $errmsg <br/>";
}
 
// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB SELECTION: ";
   echo "<br/> $errmsg <br/>";
   die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.