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 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?