//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();
/**
* Validates a string
*
* @access public
* @param $postVal - the value of the $_POST request
* @param $postName - the name of the form element being validated
* @param $min - minimum string length
* @param $max - maximum string length
* @return void
*/
public function validateStr($postVal, $postName, $min = 5, $max = 500) {
if(strlen($postVal) < intval($min)) {
$this->setError($postName, ucfirst($postName)." must be at least {$min} characters long.");
} else if(strlen($postVal) > intval($max)) {
$this->setError($postName, ucfirst($postName)." must be less than {$max} characters long.");
}
}// end validateStr
/**
* Validates an email address
*
* @access public
* @param $emailVal - the value of the $_POST request
* @param $emailName - the name of the email form element being validated
* @return void
*/
public function validateEmail($emailVal, $emailName) {
if(strlen($emailVal) <= 0) {
$this->setError($emailName, "Email 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 Email Address Required");
}
}// end validateEmail
/**
* sets an error message for a form element
*
* @access private
* @param string $element - name of the form element
* @param string $message - error message to be displayed
* @return void
*/
private function setError($element, $message) {
$this->errors[$element] = $message;
}// end logError
/**
* returns the error of a single form element
*
* @access public
* @param string $elementName - name of the form element
* @return string
*/
public function getError($elementName) {
if($this->errors[$elementName]) {
return $this->errors[$elementName];
} else {
return false;
}
}// end getError
/**
* displays the errors as an html un-ordered list
*
* @access public
* @return string: A html list of the forms errors
*/
public function displayErrors() {
$errorsList = "<ul class=\"errors\">\n";
foreach($this->errors as $value) {
$errorsList .= "<li>". $value . "</li>\n";
}
$errorsList .= "</ul>\n";
return $errorsList;
}// end displayErrors
/**
* returns whether the form has errors
*
* @access public
* @return boolean
*/
public function hasErrors() {
if(count($this->errors) > 0) {
return true;
} else {
return false;
}
}// end hasErrors
/**
* returns a string stating how many errors there were
*
* @access public
* @return void
*/
public function errorNumMessage() {
if(count($this->errors) > 1) {
$message = "There were " . count($this->errors) . " errors sending your message!\n";
} else {
$message = "There was an error sending your message!\n";
}
return $message;
}// end hasErrors
Getting no error messages in the error log. When filling out the form fields incorrectly errors should present underneath the relevant fields but nothing like that is happening! How would I know that the validate.class.php is communicating with the contact.php ?
You can put an echo statement in one of the class' methods to confirm it is being reached.
Code:
public function validateStr($postVal, $postName, $min = 5, $max = 500) {
echo 'Got inside validateStr'; die();
I installed this echo statement and got no response - so it can now be taken that the two files are not talking to each other or as you suggest.... being reached!
How do we get them to reach out to each other if the include('validate.class.php'); is not functioning as expected?
Both files in the same directory.
Have now changed validate_class.php to validateClass.php.
Sorry about the Shudder but yes the validateClass looks better.
When I run in browser or wamp still no response or errors?
if your validation class was not getting loaded you should be getting a fatal error when trying to call $v = new validate(); so something else is wrong....
at the top of contact.php add
PHP Code:
ERROR_REPORTING(E_ALL);
you may then see some other error that might give you a clue.
changing file names wont help a jot
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
Might I advance a question... why would the error process
not give any indication as to the problem that was occurring?
your form was not actually being submitted (at least not to the right place) so validate never even got a look in, at least thats my assumption. anyway glad its sorted.
MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)
Form at this link tests out fine on Wamp and presents the
message "Your message has been sent" as required after submit.
However on the web it does not show the message but goes to
an external web page.
Why is it being redirected on the web?
PHP Code:
<?php if(isset($_GET['sent'])): ?><h2>Your message has been sent</h2><?php endif; ?>
<form id="contact_form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"/>