【问题标题】:PHP display a html page if an error occurs in contact form output如果联系表单输出中发生错误,PHP 会显示一个 html 页面
【发布时间】:2016-06-08 15:57:55
【问题描述】:

我有一个带有安全问题的简单表单以防止垃圾邮件。 使用相关表单链接到我的网页http://ddry.co.uk/contact_us.html

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

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

反垃圾邮件html代码

用于发布的 php 文件。

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

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

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

【问题讨论】:

  • 试试这样的: if ($antispam != '12') { header("Location: errorpage.html"); }

标签: php html forms spam-prevention


【解决方案1】:

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

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

【讨论】:

  • 谢谢 SamyQc !!!我改变了: echo('Location: contact-failed.html#fail'); for header('位置:contact-failed.html#fail');它工作得很好。感谢您的意见:-)
  • 很高兴它有帮助:) 如果它符合您的要求,请接受我的回答!再见
【解决方案2】:

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

【讨论】:

  • 嗨,基兰。谢谢回复。这正是我想要的,问题是我缺乏实现这一目标的 php 知识。估算错误答案时如何实现重定向?成功重定向很简单,但是如果答案不正确,我将在哪里以及如何添加 fail.html 文件重定向?谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多