如果联系人表单输出中发生错误,PHP 将显示一个 html 页面


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

我有一个简单的表格,上面有一个安全问题,以防止垃圾邮件。链接到我的网页,其中包含 http://ddry.co.uk/contact_us.html 有问题的表格。

我希望能够在用户输入错误答案而不仅仅是纯文本时输出 html 页面。

如果表单在我的 php 脚本底部成功,我有一个重定向到另一个 html 文件。 查看其他论坛,有人建议使用readfile("联系人失败.html#失败"); 显示网页;但是,我不完全确定将错误答案重定向的代码放在哪里。我是PHP的新手,所以如果有人能够解释我做错了什么,那就太好了。或者,如果 somone 有更好的垃圾邮件过滤器代码,那也很棒 提前感谢。

反垃圾邮件的HTML代码

用于发布 PHP 文件。

-----更新--------

我认为我所追求的是如果,否则陈述?经过研究,我更改了我的代码以包含 else 语句;但是,由于我缺乏PHP知识,我仍然得到一个空白屏幕,而不是我的错误重定向html页面,该页面显示在我的php代码底部。

问题:如何正确配置 if, else 语句,以便如果反垃圾邮件结果错误(不等于 12),则继续联系失败.html?

提前致谢

 <?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');} 
?>

为什么不尝试这样简单的事情呢?

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');
    }

我的问题是为什么您需要在联系表单中显示另一个页面而不是错误消息。成功后您将重定向到另一个页面,同样可以应用于错误,您可以重定向到另一个html页面以显示不同的版本。