dstbp22002 2016-02-25 17:54
浏览 28
已采纳

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

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');} 
?>
  • 写回答

2条回答 默认 最新

  • dsbckxk165039 2016-02-26 14:16
    关注

    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:
     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 {
            header('Location: contact-failed.html#fail');
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 echarts动画效果失效的问题。官网下载的例子。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加