//use php's mail function to send the email
@mail($email_to, $subject ,$emailMessage ,$header );
//grab the current url, append ?sent=yes to it and then redirect to that url
$url = "http". ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
header('Location: '.$url."?sent=yes");
} else {
//set the number of errors message
$message_text = $v->errorNumMessage();
//store the errors list in a variable
$errors = $v->displayErrors();
//get the individual error messages
$nameErr = $v->getError("name");
$emailErr = $v->getError("email");
$phoneErr = $v->getError("phone");
$messageErr = $v->getError("message");
$codeErr = $v->getError("code");
} //end of the error check
}
// end isset
?>
and the "Your message has been sent" is built in at the top of the form is as follows:
PHP Code:
<?php if(isset($_POST['sent'])): ?><h2>Your message has been sent</h2><?php endif; ?>
Yes, the mail is being sent and being received in the mailbox.
The 'validateClass.php is as follows:
PHP Code:
<?php
class validate {
public $errors = array();
/* Validate message string */
public function validateStr($postVal, $postName, $min = 10, $max = 500) {
if (strlen($postVal) < intval($min)) {
$this->setError($postName, ucfirst($postName) . " at least {$min} characters long.");
} else if (strlen($postVal) > intval($max)) {
$this->setError($postName, ucfirst($postName) . " less than {$max} characters long.");
}
}
/* Validate email address */
public function validateEmail($emailVal, $emailName) {
if (strlen($emailVal) <= 0) {
$this->setError($emailName, "Address Required");
} else if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $emailVal)) {
$this->setError($emailName, "Valid Address Required");
}
}
/* Validate phone no to allow numbers only */
public function validatePhone($phoneVal, $phoneName) {
if (strlen($phoneVal) <= 0) {
$this->setError($phoneName, "Number Required");
} else if (preg_match('/[^0-9]/', $phoneVal)) {
$this->setError($phoneName, "Valid Number Required");
}
}
/* Validate captcha entry*/
public function validateCode($codeVal, $codeName) {
if (strlen($codeVal) <= 0) {
$this->setError($codeName, "Enter Validate Code");
} else if(md5($codeVal) != $_SESSION['image_random_value']) {
$this->setError($codeName, "Valid Code Required");
}
}
/* sets an error message for a form element*/
private function setError($element, $message) {
$this->errors[$element] = $message;
}
/* returns the error of a single form element*/
public function getError($elementName) {
if ($this->errors[$elementName]) {
return $this->errors[$elementName];
} else {
return false;
}
}
/* displays the errors as an html un-ordered list*/
public function displayErrors() {
$errorsList = "<ul class=\"errors\">\n";
foreach ($this->errors as $value) {
$errorsList .= "<li>" . $value . "</li>\n";
}
$errorsList .= "</ul>\n";
return $errorsList;
}
/* returns whether the form has errors*/
public function hasErrors() {
if (count($this->errors) > 0) {
return true;
} else {
return false;
}
}
/* returns a string stating how many errors there were*/
public function errorNumMessage() {
if (count($this->errors) > 1) {
$message = "There were " . count($this->errors) . " errors sending your message!\n";
} else {
$message = "Please correct the error to send your message!\n";
}
return $message;
}
}
?>
Bookmarks