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条)

报告相同问题?

悬赏问题

  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码
  • ¥15 LLM accuracy检测
  • ¥15 pycharm添加远程解释器报错