OK I tried testing my code and when I hit the submit button it goes from my employment application page to my validation page but then it goes completely blank??? I'm figuring that I either need to put my code all in the HTML form page so it re-submits back to itself. Or I need to set-up a <div> element to display my errors (not sure if I set it up properly though). Or my error array is not functioning properly. Or its most likely something I don't have a clue on (I'm going with option 4:p).
Code from employment form:
<?php
if(isset($_POST['submit']))
{
//import the validation library
include('validateForm.php');
$mandatory_fields[] = array(); //stores the validation rules
$errors[] = array(); //stores the errors
//Create post data to variables from each HTML form element
$FamilyName = trim($_POST["FamilyName"]);
$GivenName = trim($_POST["GivenName"]);
$MiddleInitial = trim($_POST["MiddleInitial"]);
$curAddress = trim($_POST["curAddress"]);
$Municipality = trim($_POST["Municipality"]);
$Region = trim($_POST["Region"]);
$PostalCode = trim($_POST["PostalCode"]);
$phoneNumber = trim($_POST["phoneNumber"]);
$EmailAddress = trim($_POST["EmailAddress"]);
$legalYes = trim($_POST["legalYes"]);//radio button
$legalNo = trim($_POST["legalNo"]);//radio button
//cut out the other post variables to save space for posting.....
//Standard form fields
$mandatory_fields['FamilyName'] = 'isAlphabet';
$mandatory_fields['GivenName'] = 'isAlphabet';
$mandatory_fields['MiddleInitial'] = 'isAlphabet';
$mandatory_fields['curAddress'] = 'validateAddress';
/*foreach ($_POST as $fieldname => $value) {
if(isset($_POST['fieldname'])) {
}
}
*/
//start validating our form
$v = new validateForm();
$v->isAlphabet($FamilyName, "FamilyName");
//Counts up all the errors
if (!$v->hasErrors()) {
//Sets the number of errors
$message = $v->errorNumMessage();
//Store the errors list in a variable
$errors = $v->displayErrors();
//Get the individual error messages
$FamilyName = $v->getError("FamilyName");
}
else
{
return true;
}
}
?>
Code from PHP validation page:
<?php
class validateForm {
//------------------------------------------------------------------
// validation methods
//------------------------------------------------------------------
/**
* Validates alphabet fields
*
*@access public
*@param
*/
public function isAlphabet($elementName) {
if(strlen($elementName) <= 0) {
$this->setError($elementName, "Please enter something into the text field.");
}
else if(preg_match("/^[a-zA-Z\\-\\., \']+$/")) {
$this->setError($elementName, "This field must consist only of letters A-Z, ' and - .");
}
else
{
return true;
}
}
/*function validateAddress($curAddress){
if(strlen($curAddress) <= 0 || strlen($curAddress) >= 50) {
$this->setError($curAddress, "Please enter address and must be no longer than 50 characters");
}
else if(preg_match("/^[a-zA-Z0-9 -.,:']+$/")) {
$this->setError("Address can only consist of letters A-Z, numbers 0-9, spaces, - , ",", :, and '.");
}
else
{
return true;
}
}
function validateZipcode($PostalCode){
if(strlen($PostalCode) <= 0 || strlen($PostalCode) >= 8) {
$this->setErrors($PostalCode, "Please enter a zip code in and can only be a max of 8 characters.");
}
else if(preg_match("/^([0-9]{5})(-[0-9]{4})?$/i")){
$this->setErrors("Zip code can only contain numbers with the format of xxxxx or xxxxx-xxxx");
}
else
{
return true;
}
}
public function validateEmail($EmailAddress){
if(strlen($EmailAddress) <= 0) {
$this->setError($EmailAddress, "Please enter in an email address");
}
else if(preg_match("/^\S+@[w\d.-]{2,}\.[\w]{2,6}$/iU")){
$this->setError($EmailAddress, "Please enter in a valid email address");
}
else
{
return true;
}
}
function validatePhoneNum($phoneNumber){
if(strlen($phoneNumber) <= 0 || strlen($phoneNumber) >= 10){
$this->setErrors($phoneNumber, "Please enter a phone number that can only be a max of 10 characters long.";
}
else if(!preg_match("/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/s")){
$this->setErrors($phoneNumber, "Please enter a valid phone number.");
}
else
{
return true;
}
}
$query = sprintf("INSERT INTO tbl (FamilyName, GivenName) Values (%s, %s)",
mysqli_real_escape_string($link, $FamilyName),
mysqli_real_escape_string($link, $GivenName)
);
*/
//---------------------------------------------------------
// Error handling methods
//---------------------------------------------------------
/**
* 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($elementName, $errors){
$this->errors[$elementName] = $errors;
}//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 are " . count($this->errors) . " errors in the form!\n";
}
else
{
return true;
}
}//end errorNumMessage
}//end validateForm class
exit;
?>
@ On the mandatory_fields array and creating a foreach statement to handle each variable I'm not sure how to go about coding it syntax-wise but I decided to do it the long way so that I can at least test out my validation functions to see that they're working properly. I found a decent example of the concept you mentioned but it was different in certain ways (PHP example: https://github.com/benkeen/php_validation).