if ($_POST['process'] == 1) {
$pattern = '/.*@.*\..*/';
$email = $_POST['email'];
$fname = $_POST['fName'];
$lname = $_POST['lName'];
$year = $_POST['year'];
$id = $_POST['idNum'];
$reason = $_POST['explain'];
$fdate=$_POST["firstDate"];
$sdate=$_POST["lastDate"];
$counter =1;
/**
*
*if email address is not fine, then redirect to next page called
*thank you.php
*
**/
//validate_date($fdate);
//validate_date($sdate);
$date_parts = explode("/", $fdate);
if(checkdate($date_parts[1], $date_parts[0], $date_parts[2])){
$counter =0;
}else{
$dateMess = "Date must be in DD/MM/YYYY format";
$counter =$counter+1;
}
$date_parts = explode("/", $sdate);
if(checkdate($date_parts[1], $date_parts[0], $date_parts[2])){
$counter =0;
}else{
$sdateMess = "Date must be in DD/MM/YYYY format";
$counter =$counter+1;
}
if (!preg_match($pattern, $_POST['email']) > 0) {
$message = "Please enter a valid email address.";
return $counter =$counter+1;
}else{
$counter =0;
}
if(preg_match("/[^A-Za-z]/",$fname))
{
$nameMessage = "No numbers are allowed in this field.";
$counter =$counter+1;
}else{
$counter =0;
}
if(strlen($fname)==0)
{
$nameMessage = "Please enter your name";
$counter =$counter+1;
}else{
$counter =0;
}
if(preg_match("/[^A-Za-z]/",$lname))
{
$lnameMessage = "No numbers are allowed in this field.";
$counter =$counter+1;
}else{
$counter =0;
}
if(strlen($lname)==0)
{
$lnameMessage = "Please enter your name";
$counter =$counter+1;
}else{
$counter =0;
}
if($year=="Please")
{
$yearStudy = "Please select an option from the drop down list";
$counter =$counter+1;
}else{
$counter =0;
}
if(preg_match("/[^0-9]/",$id))
{
$idMessage = "Only numbers are allowed in this field.";
$counter =$counter+1;
}else{
$counter =0;
}
if(strlen($id)!=8)
{
$idMessage = "Student ID Number has to be 8 digits";
$counter =$counter+1;
}else{
$counter =0;
}
if(strlen($reason)==0)
{ $reasonMessage = "Textfield is empty";
$counter =$counter+1;
}else{
$counter =0;
}
echo $counter;
if($counter<1)
{
//mail( "sams@hotmail.com", "Feedback Form Results",
//$idMessage, "From: $email" );
header( "Location: http://www.example.com/thankyou.html" );
}
}
I have a variable called:
$counter =1;
which is a flag. Everytime an item in each field on the form is incorrectly inputed it accumulates. If it does not accumulate at all the the last if statement is invoked, causing it to go to the thank you page.
Problem I am having is that the var counter does not remember its previous value after the if statements the next if statements are invoked.
How do I sort this one out cheers.