Here is the original script:
[php]
<?php
// This is my Validation Code //
$form = new ProcessForm();
$form->field_rules = array(
'groupname'=>'required',
'name1'=>'required',
'name2'=>'required',
'address'=>'required',
'city'=>'required',
'state'=>'required',
'zip'=>'required',
'homephone'=>'required',
'cellphone'=>'required',
'email'=>'required',
'agegroup'=>'required',
'maritalstatus'=>'required',
'income'=>'required',
'date1'=>'required',
'date2'=>'required',
'date3'=>'required'
);
$form->validate();
class ProcessForm
{
public $field_rules;
public $error_messages;
public $fields;
private $error_list;
private $is_xhr;
function __construct()
{
$this->error_messages = array(
'required' => 'This field is required',
'email' => 'Please enter a valid email address',
'number' => 'Please enter a numeric value',
'url' => 'Please enter a valid URL',
'pattern' => 'Please correct this value',
'min' => 'Please enter a value larger than the minimum value',
'max' => 'Please enter a value smaller than the maximum value'
);
$this->field_rules = array();
$this->error_list = '';
$this->fields = $_POST;
$this->is_xhr = $this->xhr();
}
function validate()
{
if (!empty($this->fields))
{
//Validate each of the fields
foreach ($this->field_rules as $field => $rules)
{
$rules = explode('|', $rules);
foreach ($rules as $rule)
{
$result = null;
if (isset($this->fields[$field]))
{
$param = false;
if (preg_match("/(.*?)\[(.*?)\]/", $rule, $match))
{
$rule = $match[1];
$param = $match[2];
}
$this->fields[$field] = $this->clean($this->fields[$field]);
//if the field is a checkbox group create string
if (is_array($this->fields[$field]))
$this->fields[$field] = implode(', ', $this->fields[$field]);
// Call the function that corresponds to the rule
if (!empty($rule))
$result = $this->$rule($this->fields[$field], $param);
// Handle errors
if ($result === false)
$this->set_error($field, $rule);
}
}
}
if (empty($this->error_list))
{
if ($this->is_xhr)
echo json_encode(array('status' => 'success'));
$this->process();
}
else
{
if ($this->is_xhr)
echo json_encode(array('status' => 'invalid', 'errors' => $this->error_list));
else echo $this->error_list;
}
}
}
function process()
{
/**
* SUCCESS!!
* There were no errors in the form. Insert your processing logic here (i.e. send an email, save to a
* database etc.
*
* All of the submitted fields are available in the $this->fields variable.
*
* Example code to mail the results of the form:
*
* $msg = "Form Contents: \n\n";
* foreach($this->fields as $key => $field)
* $msg .= "$key : $field \n";
*
* $to = '
[email protected]';
* $subject = 'Form Submission';
* $from = '
[email protected]';
*
* mail($to, $subject, $msg, "From: $from\r\nReply-To: $from\r\nReturn-Path: $from\r\n");
*
*/
}
function set_error($field, $rule)
{
if ($this->is_xhr)
{
$this->error_list[$field] = $this->error_messages[$rule];
}
else $this->error_list .= "
$field: " . $this->error_messages[$rule] . "
";
}
function xhr()
{
return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') ? true : false;
}
/** Validation Functions */
function required($str, $val = false)
{
if (!is_array($str))
{
$str = trim($str);
return ($str == '') ? false : true;
}
else
{
return (!empty($str));
}
}
function email($str)
{
return (!preg_match("/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD", $str)) ? false : true;
}
function number($str)
{
return (!is_numeric($str)) ? false : true;
}
function min($str, $val)
{
return ($str >= $val) ? true : false;
}
function max($str, $val)
{
return ($str <= $val) ? true : false;
}
function pattern($str, $pattern)
{
return (!preg_match($pattern, $str)) ? false : true;
}
function clean($str)
{
$str = is_array($str) ? array_map(array("ProcessForm", 'clean'), $str) : str_replace('\\', '\\\\', strip_tags(trim(htmlspecialchars((get_magic_quotes_gpc() ? stripslashes($str) : $str), ENT_QUOTES))));
return $str;
}
}
// This is my Insert to Database Code //
define('DB_NAME', 'database');
define('DB_USER', 'username');
define('DB_PASSWORD', '[password');
define('DB_HOST', 'localhost');
$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
if (!$link)
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(DB_NAME, $link);
if (!$db_selected) {
die('Can\'t use ' . DB_NAME . ': ' . mysql_error());
}
$value1 = $_POST['groupname'];
$value2 = $_POST['name1'];
$value3 = $_POST['name2'];
$value4 = $_POST['address'];
$value5 = $_POST['city'];
$value6 = $_POST['state'];
$value7 = $_POST['zip'];
$value8 = $_POST['homephone'];
$value9 = $_POST['cellphone'];
$value10 = $_POST['email'];
$value11 = $_POST['age'];
$value12 = $_POST['maritalstatus'];
$value13 = $_POST['income'];
$value14 = $_POST['contact1'];
$value15 = $_POST['contact2'];
$value16 = $_POST['contact3'];
$value17 = $_POST['date1'];
$value18 = $_POST['date2'];
$value19 = $_POST['date3'];
$value20 = $_POST['gift'];
$sql = "INSERT INTO databasename (groupname, name1, name2, address, city, state, zip, homephone, cellphone, email, age, maritalstatus, income, contact1, contact2, contact3, date1, date2, date3, gift) VALUES ('$value1', '$value2', '$value3', '$value4', '$value5', '$value6', '$value7', '$value8', '$value9', '$value10', '$value11', '$value12', '$value13', '$value14', '$value15', '$value16', '$value17', '$value18', '$value19', '$value20')";
if (!mysql_query($sql)) {
die('Error: ' . mysql_error());
}
mysql_close();
// This is my Excel File Creation Code //
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$sql = "SELECT * FROM databasename.tablename tablename ORDER BY groupname ASC";
// Query Database
$result=mysql_query($sql);
$filename = 'filename.xls';
// XLS Data Cell
ob_start(); // start the file
xlsBOF();
xlsWriteString(0,0,"Group");
xlsWriteString(0,1,"Name 1");
xlsWriteString(0,2,"Name 2");
xlsWriteString(0,3,"Address");
xlsWriteString(0,4,"City");
xlsWriteString(0,5,"State");
xlsWriteString(0,6,"Zip Code");
xlsWriteString(0,7,"Home Phone");
xlsWriteString(0,8,"Cell Phone");
xlsWriteString(0,9,"E-mail Address :");
xlsWriteString(0,10,"Age Group");
xlsWriteString(0,11,"Marital Status");
xlsWriteString(0,12,"Income");
xlsWriteString(0,13,"Contact Via");
xlsWriteString(0,14,"Dates");
xlsWriteString(0,15,"Gift Choice");
$xlsRow = 2;
while(list($groupname, $name1, $name2, $address, $city, $state, $zip, $homephone, $cellphone, $email, $age, $maritalstatus, $income, $contact1, $contact2, $contact3,$date1, $date3, $date3, $gift)=mysql_fetch_row($result)) {
++$i;
xlsWriteString($xlsRow,0,"$groupname");
xlsWriteString($xlsRow,1,"$name1");
xlsWriteString($xlsRow,2,"$name2");
xlsWriteString($xlsRow,3,"$address");
xlsWriteString($xlsRow,4,"$city");
xlsWriteString($xlsRow,5,"$state");
xlsWriteString($xlsRow,6,"$zip");
xlsWriteString($xlsRow,7,"$homephone");
xlsWriteString($xlsRow,8,"$cellphone");
xlsWriteString($xlsRow,9,"$email");
xlsWriteString($xlsRow,10,"$age");
xlsWriteString($xlsRow,11,"$maritalstatus");
xlsWriteString($xlsRow,12,"$income");
xlsWriteString($xlsRow,13,"$contact1, $contact2, $contact3");
xlsWriteString($xlsRow,14,"$date1, $date3, $date3");
xlsWriteString($xlsRow,15,"$gift");
$xlsRow++;
}
xlsEOF(); // $filename should be set to some writeable location
file_put_contents("info/" . $filename, ob_get_clean());
function xlsBOF() {
echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
return;
}
function xlsEOF() {
echo pack("ss", 0x0A, 0x00);
return;
}
function xlsWriteNumber($Row, $Col, $Value) {
echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
echo pack("d", $Value);
return;
}
function xlsWriteString($Row, $Col, $Value ) {
$L = strlen($Value);
echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
echo $Value;
return;
}
// This is my Mail Code //
include("class.phpmailer.php");
include("class.smtp.php"); // note, this is optional - gets called from main class if not already loaded
$mail = new PHPMailer();
$body = file_get_contents('contents.html');
$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP();
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPSecure = "ssl"; // sets the prefix to the servier
$mail->Host = "smtp.gmail.com"; // sets GMAIL as the SMTP server
$mail->Port = 465; // set the SMTP port
$mail->Username = "
[email protected]"; // GMAIL username
$mail->Password = "password"; // GMAIL password
$mail->From = '
[email protected]';
$mail->FromName = "Site Info";
$mail->Subject = "New Client Signup";
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
$mail->WordWrap = 50; // set word wrap
$mail->MsgHTML($body);
$mail->AddReplyTo('
[email protected]');
$mail->AddAttachment("filename.xls"); // attachment
$mail->AddAddress("
[email protected]");
$mail->IsHTML(true); // send as HTML
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header("Location: thankyou.html");
}
?>[/php]
And the validation works…But it still continues with the rest of the script. Meaning it loads the empty fields into the sql database, and into the xls file.
What is it printing is:
groupname: This field is required
name1: This field is required
name2: This field is required
address: This field is required
city: This field is required
state: This field is required
zip: This field is required
homephone: This field is required
cellphone: This field is required
email: This field is required
maritalstatus: This field is required
income: This field is required
date1: This field is required
date2: This field is required
date3: This field is required
That is when I want the script to die. So It all works i just need the script to stop after it finds that some fiels are empty.