PHP 如果在联系表单输出中出现错误,则显示 html 页面

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

我有一个带有安全问题的简单表格来防止垃圾邮件。 link 使用相关表格 http://ddry.co.uk/contact_us.html.

到我的网页

如果用户输入的答案不正确,而不仅仅是纯文本,我希望能够输出 html 页面。

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

html反垃圾邮件代码

php 文件 post。

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

我想我想要的是 if, else 语句? 经过研究,我修改了我的代码以包含一个 else 语句;然而,由于我缺乏 PHP 知识,我仍然得到一个空白屏幕,而不是我的错误重定向 html 页面,它显示在我的 php 代码的底部。

问题:如果反垃圾邮件结果错误(不等于 12),如何正确配置 if、else 语句,然后继续联系-failed.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');} 
?>

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

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

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