I have a simple form with a security question to prevent spamming. link to my webpage with the form in question http://ddry.co.uk/contact_us.html.
I want to be able to output an html page if a user inputs an incorrect answer rather than just plain text.
I have a redirect to another html file if the form is successful at the bottom of my php script. looking at other forums someone suggested using readfile("contact-failed.html#fail"); to display the html; However I'm not entirely sure where to put the code for the redirect of an incorrect answer. I'm new to PHP, so if someone is able to explain what I'm doing wrong that would great. Or alternately if somone has a better spam filter code that would be great also Thanks in advance.
html code of anti spam
php file for post.
----- UPDATE --------
I think what i'm after is an if, else statement? after researching I have altered my code to include an else statement; However due to my lack of PHP knowledge I'm still getting a blank screen instead of my error redirect html page, which is shown at the bottom of my php code.
Question: how can I properly configure the if, else statement so if the anti-spam result is wrong (doesn't equal to 12) then proceed to contact-failed.html?
Thanks in advance
<?php
// Email address verification
function isEmail($clientEmail) {
return filter_var($clientEmail, FILTER_VALIDATE_EMAIL);}
if($_POST) {
// Enter the email where you want to receive the message
$myemail = 'info@ddry.co.uk';
$name = addslashes(trim($_POST['name']));
$clientEmail = addslashes(trim($_POST['email']));
$subject = addslashes(trim($_POST['phone']));
$phone = addslashes(trim($_POST['phone']));
$message = addslashes(trim($_POST['message']));
$antispam = addslashes(trim($_POST['antispam']));
$array = array('nameMessage' => '','emailMessage' => '', 'phoneMessage' => '', 'messageMessage' => '', 'antispamMessage' => '');
if(!isEmail($clientEmail)) {
$array['nameMessage'] = 'Empty name';
}
if(!isEmail($clientEmail)) {
$array['emailMessage'] = 'Invalid email!';
}
if($phone == '') {
$array['phoneMessage'] = 'Empty phone number!';
}
if($message == '') {
$array['messageMessage'] = 'Empty message!';
}
if($antispam != '12') {
$array['antispamMessage'] = 'Incorrect Answer!';
}
if(isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam == '12') {
// Send email
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:
Name: $name
".
"Email: $clientEmail
Message:
$message
Phone: $phone";
$headers = "From: $myemail
";
$headers .= "Reply-To: $clientEmail";
mail($to,$email_subject,$email_body,$headers);
echo json_encode($array);
header('Location: contact-success.html#success');
}
else (isEmail($clientEmail) && $clientEmail != '' && $message != '' && $antispam !== '12'){
echo('Location: contact-failed.html#fail');}
?>