Forum Moderators: coopster
<?php
session_start();
if (count($_POST) > 0)
{
$errors = 0; // number of validation errors found
switch($_POST['formname'])
{
case 'pg1': // Validate the fields of page 1
if (!$_POST['comp_name']) {
$errors++;
$error_text[$errors] = "Company Name is a required field.";
}
if (!$_POST['address']) {
$errors++;
$error_text[$errors] = "Address is a required field.";
}
if (!$_POST['city']) {
$errors++;
$error_text[$errors] = "City is a required field.";
}
if (!$_POST['state']) {
$errors++;
$error_text[$errors] = "State is a required field.";
}
if (!$_POST['zipcode']) {
$errors++;
$error_text[$errors] = "Zip Code is a required field.";
}
if (!$_POST['phone']) {
$errors++;
$error_text[$errors] = "Phone Number is a required field.";
}
if (!$_POST['fax']) {
$errors++;
$error_text[$errors] = "Fax Number is a required field.";
}
if (!$_POST['adminf']) {
$errors++;
$error_text[$errors] = "Administrators First Name is a required field.";
}
if (!$_POST['adminl']) {
$errors++;
$error_text[$errors] = "Administrator Last Name is a required field.";
}
if (!$_POST['admin_email']) {
$errors++;
$error_text[$errors] = "Email Address is a required field.";
}
if (!strstr($_POST['admin_email'], '@') ¦¦!strstr($_POST['admin_email'], '.'))
{
$errors++;
$error_text[$errors] = "The email address given is not valid. Please give a real email address.";
}
break;
case 'pg2': // Validate the fields of page 2
if (!$_POST['prac_name']) {
$errors++;
$error_text[$errors] = "Practice Name is a required field.";
}
if (!$_POST['prac_address']) {
$errors++;
$error_text[$errors] = "Address is a required field.";
}
if (!$_POST['prac_city']) {
$errors++;
$error_text[$errors] = "City is a required field.";
}
if (!$_POST['prac_state']) {
$errors++;
$error_text[$errors] = "State is a required field.";
}
if (!$_POST['prac_zipcode']) {
$errors++;
$error_text[$errors] = "Zip Code is a required field.";
}
if (!$_POST['prac_phone']) {
$errors++;
$error_text[$errors] = "Phone is a required field.";
}
if (!$_POST['prac_speciality']) {
$errors++;
$error_text[$errors] = "Speciality is a required field.";
}
break;
case 'pg3': // Validate the fields of page 3
// ...
break;
}
if (!$errors)
{
// Add Fields to Session (You want to make sure this happens AFTER validation)
$_SESSION['rgform'][$_POST['formname']] = $_POST;
/* Why store each page separately?
Unfortunately, checkbox items only get submitted when they are checked. Nothing gets submitted
for an unchecked checkbox.
If you simply merged new elements to a single array after each page was submitted,
only the submitted elements would be overwritten, so you would have no way of removing
a checkbox item that had previously been checked.
Therefore, we must completely erase and rewrite all form elements stored in the session
each time those form elements are submitted. By storing each page's elements in
separate arrays, we can accomplish this with the single line of code above.
*/
// In this particular process, we know hat pg3 is the last page, so we can process all the data once pg3 is submitted
mysql_connect(localhost,cg1506_cg1506,cecilia);
@mysql_select_db(cg1506_cgsaleforms) or die( "Unable to select database");
if ($_POST['formname'] == "pg1")
//Post Page 1 Information to Database
{
$comp_name=$_POST['comp_name'];
$address=$_POST['address'];
$city=$_POST['city'];
$state=$_POST['state'];
$zipcode=$_POST['zipcode'];
$phone=$_POST['phone'];
$fax=$_POST['fax'];
$adminf=$_POST['adminf'];
$adminl=$_POST['adminl'];
$admin_email=$_POST['admin_email'];
$sales_rep=$_POST['sales_rep'];
$ipaddress = getenv("REMOTE_ADDR");
$timestamp=date('YmdHis');
$query = "INSERT INTO company_info VALUES ('$id', '$comp_name', '$address', '$city', '$state', '$zipcode', '$phone', '$fax', '$adminf', '$adminl', '$admin_email', '$sales_rep', '$ipaddress', '$timestamp')";
mysql_query($query);
}
if ($_POST['formname'] == "pg2")
//Post Page 2 Information to Database
{
$prac_name=$_POST['prac_name'];
$prac_address=$_POST['prac_address'];
$prac_city=$_POST['prac_city'];
$prac_state=$_POST['prac_state'];
$prac_zipcode=$_POST['prac_zipcode'];
$prac_phone=$_POST['prac_phone'];
$prac_paddress=$_POST['prac_paddress'];
$prac_pcity=$_POST['prac_pcity'];
$prac_pstate=$_POST['prac_pstate'];
$prac_pzipcode=$_POST['prac_pzipcode'];
$prac_speciality=$_POST['prac_speciality'];
$id = mysql_insert_id();
$query = "INSERT INTO practice_info VALUES ('$id', '$prac_name', '$prac_address', '$prac_city', '$prac_state', '$prac_zipcode', '$prac_phone', '$prac_paddress', '$prac_pcity', '$prac_pstate', '$prac_pzipcode', '$prac_speciality')";
mysql_query($query);
}
if ($_POST['formname'] == "pg3")
{
mysql_close();
}
// Forward to next page
header("location: {$_SERVER['PHP_SELF']}?page={$_POST['nextpage']}");
}
else
{
// Redisplay this form with the values repopulated from the previous post
$fv = &$_POST;
}
}
// Send headers that make sure these pages don't get cached with old data
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); # Date in Past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); # Always Modified
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: get-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");
?>
[edited by: jatar_k at 4:35 pm (utc) on Sep. 20, 2006]
[edit reason] fixed sidescroll [/edit]
There are three ways around this:-
1) Set a session variable after inserting form1
2) Set a cookie after inserting form1
3) Add a type='hidden' form field to form2 when it is output with the content of $id as the value - this means form2 must be output AFTER form1 is inserted (but still in the same page-load)
Once finished, I use an UPDATE to add the rest of the data. The only downfall is that partial submissions are possible but that is not an issue.