简体   繁体   中英

PHP display a html page if an error occurs in contact form output

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:\n Name: $name \n ".
 "Email: $clientEmail\n Message: \n $message\n Phone: $phone";
 $headers = "From: $myemail\n";
 $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');} 
?>

Why not try something simple like this ?

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']));
    if($antispam == '12' && isEmail($clientEmail) && $phone != '' && $message != ''   ){
        $to = $myemail;
        $email_subject = "Contact form submission: $name";
        $email_body = "You have received a new message. ".
        " Here are the details:\n Name: $name \n ".
        "Email: $clientEmail\n Message: \n $message\n Phone: $phone";
        $headers = "From: $myemail\n";
        $headers .= "Reply-To: $clientEmail";
        mail($to,$email_subject,$email_body,$headers);
        echo json_encode($array);
        header('Location: contact-success.html#success'); 
    }
    else {
        header('Location: contact-failed.html#fail');
    }

My question is why you need to show another page instead of the error message in the contact form. You are redirecting to another page on success, the same can be applied on error also, you can redirect to another html page to show different version .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM