dongqian9567 2014-04-04 22:52
浏览 60

我的PHP验证和电子邮件表单直接进入我的确认页面

I have a form, php validation, and send to email. My php validation works fine. My send to email works fine. When I use them both together, they work fine until I add header('Location: http://google.com'); exit(); I am using google.com for because I havent made my confirmation page yet. When I add this line to the php, that's when it goes straight to google.com when I go to my website. Can someone please help? I have been trying to figure out all of this validation and form to email for 2 straight days now, and I cannot figure it out. I know nothing about php. My code is below.

My php:

<?php

// define variables and set to empty values
$nameErr = $emailErr = $email2Err = $commentsErr = "";
$name = $email = $email2 = $comments = "";

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if (empty($_POST["name"])) {
        $nameErr = "Name is required";
    } else {
        $name = test_input($_POST["name"]);
        // check if name only contains letters and whitespace
        if ( ! preg_match("/^[a-zA-Z ]*$/", $name)) {
            $nameErr = "Only letters and white space allowed"; 
        }
    }
    if (empty($_POST["email"])) {
        $emailErr = "Email is required";
    } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address syntax is valid
        if ( ! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
            $emailErr = "Invalid email format"; 
        }
    }
    if (empty($_POST["email2"])) {
        $email2Err = "It is required to re-enter your email.";
    } else {
        $email2 = test_input($_POST["email2"]);
        // check if e-mail address syntax is valid
        if ( ! preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email2)) {
            $email2Err = "Invalid email format"; 
        }
    }
    if (empty($_POST["comments"])) {
        $commentsErr = "A comment is required.";
    } else {
        $comments = test_input($_POST["comments"]);
        if (preg_match("#^[a-zA-Z0-9 \.,\?_/'!£\$%&*()+=
-]+$#", $comments)) {
            // Everything ok. Do nothing and continue 
        } else { 
            $commentsErr = "Message is not in correct format.<br>You can use a-z A-Z 0-9 . , ? _                   / ' ! £ $ % * () + = - Only";  
        }
    }
    if (isset($_POST['service'])) {
        foreach ($_POST['service'] as $selectedService)
            $selected[$selectedService] = "checked";
    }
}
if (empty($errors)) { 
    $from = "From: Our Site!"; 
    $to = "jasonriseden@yahoo.com"; 
    $subject = "Mr Green Website | Comment from " . $name . "";

    $message = "Message from " . $name . " 
      Email: " . $email . " 
      Comments: " . $comments . "";
    mail($to, $subject, $message, $from);
    header('Location: http://google.com');
    exit();
}
?>

Please someone help me. I have no idea what is wrong.

Ok. I did what you told me Barmar. Not sure if I did it right or not. It solved one problem, but another was created.

I started over with the code that validates and sends the form data to my email. Now I just want to add header('Location: http://google.com '); exit(); ....and it work. Can you tell me what to do? I have no idea what php, so the more specific that you can be, the better.

Here is the php:

<?php

// define variables and set to empty values
$nameErr = $emailErr = $email2Err = $commentsErr = "";
$name = $email = $email2 = $comments = "";

if ($_SERVER["REQUEST_METHOD"] == "POST")
{



 if (empty($_POST["name"]))
     {$nameErr = "Name is required";}
   else
     {$name = test_input($_POST["name"]);
      // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name))
      {
      $nameErr = "Only letters and white space allowed"; 
      }
    }


   if (empty($_POST["email"]))
     {$emailErr = "Email is required";}
   else
     {$email = test_input($_POST["email"]);
     // check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
      {
      $emailErr = "Invalid email format"; 
      }
    }

   if (empty($_POST["email2"]))
     {$email2Err = "It is required to re-enter your email.";}
   else
     {$email2 = test_input($_POST["email2"]);
     // check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email2))
      {
      $email2Err = "Invalid email format"; 
      }
    }

    if (empty($_POST["comments"]))
     {$commentsErr = "A comment is required.";}
   else
     {$comments = test_input($_POST["comments"]);
      if (preg_match("#^[a-zA-Z0-9 \.,\?_/'!£\$%&*()+=
-]+$#", $comments)) { 
        // Everything ok. Do nothing and continue 
    } else { 
        $commentsErr = "Message is not in correct format.<br>You can use a-z A-Z 0-9 . , ? _ / ' ! £ $ % * () + = - Only";  
      }
    }
     if (isset($_POST['service']))
    {
        foreach ($_POST['service'] as $selectedService)
            $selected[$selectedService] = "checked";
    }

}

function test_input($data)
{
     $data = trim($data);
     $data = stripslashes($data);
     $data = htmlspecialchars($data);
     return $data;
}

if (empty($errors)) { 
  $from = "From: Our Site!"; //Site name
  // Change this to your email address you want to form sent to
  $to = "jasonriseden@yahoo.com"; 
  $subject = "Mr Green Website | Comment from " . $name . "";

  $message = "Message from " . $name . " 
  Email: " . $email . " 
  Comments: " . $comments . "";
  mail($to,$subject,$message,$from);

  }
?>
  • 写回答

2条回答 默认 最新

  • doubai9014 2014-04-04 23:08
    关注

    The problem is that there's no variable $errors. So if(empty($errors)) is always true, so it goes into the block that sends email and redirects. This happens even if the user hasn't submitted the form yet -- I'm assuming this code is part of the same script that displays the registration form after the code you posted.

    You need to make two changes:

    1. The code that sends the email and redirects should be moved inside the first if block, after all the validation checks.

    2. Instead of if(empty($error)), it should check if($nameErr && $emailErr && $email2Err && $commentsErr). Or you should change the validation code to set $error whenever it's setting one of these other error message variables.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题