ErnieAlex I hope you are listening :) Need help stopping php script.

ErnieAlex you helped me so much last time with inserting info into mysq database then creating an xls file and emailing to me. I think this one should be easy.

What I want the script to do at the start is validate that all the fields are filled in and if they are then run the rest of the script. If some of the required fields are not filled in I wan’t it to show the error message and then stop.

As it sits now if all the fields are not filled in it shows the error message but still loads the info into the sql table and creates the excel file and then emails it with the blank fields. I need it to stop at that point and just show the error and not run the rest of the script.

Here is the code NOW:

[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]

I’m not Ernie, however during validation if there is an error set $is_error to true, then throw the query, and mail functions etc into an if statement… if (!$is_error) { rest of code runs inside } Unless I’m misunderstanding something?

Unfortunately I am not all that familiar with php. So I wouln’t know where to even start.

But…What I want it to do. Is when the validation error occurs from them not inputing info in all the form fields I want the script to stop (essentially die) at that point and not continue with the rest of the script.

I don’t know what to put i - or where to put it in the script.

Sadly I’m not familiar with using class… but I’m assuming if there is an empty field $form->validate() will return that there is an error… in which case you would use the if statement to either run or kill the script… If you’re familiar with that this might help otherwise I’m going to have to leave this to someone more familiar with classes sorry.

I just looked through your code, and have a couple of ways of solving this problem. What I need to know though is, does your form print out the error list or is this code going to have to print the error list before it dies?

The code is going to print the error then I want it to die.

As it is now the php file prints out:

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
age: 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

But then continues to load the empty fields into the sql database and create an xls file with empty fields.

try this

[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; } } if (!empty($this->error_list)) { return; } else { // 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]

well not its prints:

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

Fatal error: Using $this when not in object context in /home/fundtour/public_html/database.php on line 209

Even when I fill out all of the fields in the html form and hit submit it throws the fatal error below and loads nothing into the sql database.

Fatal error: Using $this when not in object context in /home/fundtour/public_html/database.php on line 209

LINE: if (!empty($this->error_list))

my mistake for overlooking the class

[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; } } if (!empty($form->error_list)) { return; } else { // 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]

sorry about that, ignore my last post

[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 elist() { return $this->error_list; } 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; } } if (!empty($form->elist())) { return; } else { // 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]

Well now it my program says there is a syntax error on line 396 which is ?> When I remove the bracket at the very bottom of the “validation script” right before the “instert into database” script so it reads:
[php]
if (!empty($form->elist()))
{
return;
}
else
[/php]

Then the syntax error in the program goes away. But when the script is run it throws:

Fatal error: Can’t use method return value in write context in /home/site/public_html/script.php on line 197

LINE: if (!empty($form->elist()))

Sorry for joining late in the group post… Have not been around… Agon024, looks like you have had some good help so far.

Well, took a quick look at your code. I do not understand the process order you have. You have a large number of functions that take care of validation. During this you have a function called “process()” which is where the actual processing of the live data is handled if all else is validated. This function has no processing in it, it just returns. ???

Then, after all of the functions you have code checking the “elist” and if that is empty, you do processing. Did I miss something, or should all of your processing code be inside the function “process()”???

So, what I am saying is that at the end of your entire code, you have your “mail-code”. So, this is NOT inside of any function. It will run no matter what happens inside the various functions. Normally, you would have all of your functions in a separate file and that would be an included file. The only live code would be the ones that call the functions. So, no matter what happens in the validation functions, your email would still be sent either way! I think you want to put that email code into the “Process()” function. Did that make sense?

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.

Well, Agon024, that is what I just said in my last post…

You process your code no matter what happens in the validation. That is incorrect.

In your code:
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
header(“Location: thankyou.html”);
}
You send the mail no matter what and if no error there, you say thank-you.
You should change this section to check for errors and print them or send the mail and say thank-you.
So, the validation should be called, then check for the results. If results are positive (validation errors found),
then do not bother building the email, a waste of time. If no validation errors, then build the email and send it and thank-you… Did that make sense?

So, this is more how it should be. (You will have to figure it out!)
Basically, it your code, you have: $form->validate(); which takes the form and validates it. that validate function should return the errors if any. So, you should do it more like this:
if($form->validate()) {
// Do your database insert here…
// Do your email sending and thank-you here…
}else{
// Do your error listing print out here…
}
And, that should be all … So, if you remove all of your validation code from the page you posted and put it into an include file, you can use that validation code in other projects. All you would need in the items code above filled in with your database, email and error-display sections… Did that make sense?

Just did what I should have done from the beginning and used java instead of php to validate the fom. So its all working fine now.

Sponsor our Newsletter | Privacy Policy | Terms of Service