Your conditions are wrong, you have:
strlen($name > 31)
This will first check if $name
has a value higher than 31, which returns a boolean value of true
or false
. This is then cast to a string and the length of it is checked, which will always be higher than 0 and thus be true. It should be:
strlen($name) > 31
To prevent the e-mail from being sent, you have to abort after these conditions are true. Your code would become something like:
if (strlen($name) > 31 || strlen($lname) > 31 || strlen($organisation) >31 || strlen($email)>51 || strlen($subject)>31 || strlen($reference)>31 || strlen($message)>2001){
echo"sorry, max length for a field has been exceeded. Go back and try again</a><br><br>";
} else {
$to='skyrocketing132@yahoo.com';
$emailsubject=$subject;
$body=$name."\nCompany Name: ".$organisation."\nRef: ".$reference."\nMessage: ".$message;
$headers= 'From: '.$email;
// mails, if statement so if its true (mail did send)
if (mail($to,$emailsubject,$body,$headers)){
echo'Thanks for contacting us.';
}else{
echo'Sorry, an error occurred. Try again later.';
}
}