I have a php form that I run a test to see if they have entered in all required fields before inserting into a MySQL db. The PHP script simply reports what is missing and exits the rest of the page. However, when you click the back button, all the information that was entered, is gone. Is there a simple way of retaining this information so the user doesn't has to retype all the information in?
If you had a MySQL connection, I think that the easiest (also most safe and stable) solution would be putting the POST-data and error-report to session/temporary mysql-table. I've used this kind of solution on several projects and it has work pretty well. In this you'll also need to set some information to cookies so user must have cookies enabled on his/her browser.
* MySQL-table
I've been using specifid session-table in my projects, which also carries logged users sessionid and other session-data. But you can also create specific temp-table for incomplete form-data. It could be something like this
[sql]
CREATE TABLE temp_form_data ( id int(10) UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY, stamp int(10) NOT NULL, data blob NOT NULL, error blob NOT NULL );
[/sql]
* Form prosessing
Then in the form prosessing you could make some kind of error array where to include error information. If any errors occur, save post and error data to mysql and set cookie with mysqls mysql_insert_id()
Example: [code]
$error = array();
// validate fields how you like, I do regexps.
// check for email-address field.
if (!preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $_POST['mail'], $res))
{
// make an new index to $error array, you can assign any value
// you like for index.
$error['mail'] = 1;
}
// check if any errors occured if (count($error) > 0) { // make a serialized dump of both $_POST and $error array
$dump_data = addslashes(serialize($_POST)); $dump_error = addslashes(serialize($error));
// build a sql-query to update form and error data to mysql // table.
$sql = "INSERT INTO temp_form_data "; $sql .= "(date, data, error) "; $sql .= "VALUES (UNIX_TIMESTAMP(),'{$dump_data}','{$dump_error}')";
// execute if (mysql_query($sql)) { // set cookie setcookie('temp_form_data_id', mysql_insert_id());
// get user back to form. // IMPORTANT: this must be before anything has been printed else // it won't work. header ("Location: form.php"); } else { print "MySQL Error: " . mysql_error(); } } else { // FORM IS VALIDATED SUCCESFULLY // insert data }
[/code]
* Form itself
In form you have to first check if "temp_form_data_id" cookie exists. If does, you should load saved form data from db. After loading data you should check before every field does error array have index matching fields name. You should also assign values to fields from db.
Here's an example: [code]
if (isset($_COOKIE['temp_form_data_id']) && !empty($_COOKIE['temp_form_data_id']))
{
$id = $_COOKIE['temp_form_data_id'];
// select $sql = "SELECT * "; $sql .= "FROM temp_form_data "; $sql .= "WHERE id = {$id} ";
// execute if ($q = mysql_query($sql)) { // fetch row $result = mysql_fetch_row($q);
// unserialize $error = unserialize(stripslashes($result['error'])); $data = unserialize(stripslashes($result['data'])); else { print "MySQL Error: " . mysql_error(); } }
if ($error) print '<h3>Form was filled incorrectly</h3>';
print '<form action="processer.php" method="post">'; if ($error['mail']) print '<b>Error: user valid email address</b><br>'; print 'Email address:<br>'; print sprintf('<input type="text" name="mail" value="%s">', $data['mail']); print '<input type="submit" value="ok"></form>';
[/code]
This is how it should work. I hope you get the picture.
Cheers, Joona.
-- Joona Kulmala <[EMAIL PROTECTED]> PHP Finland
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php