我希望这个验证,如果没有错误邮寄出答案。

时间:2014-11-11 19:27:50

标签: php html validation

我是php的新手,正在帮助非营利组织对他们的志愿者进行退出调查。我已经在我的第一个html标记上方的空间中构建了以下脚本,并且可以在字段为空时让它在提交时向我显示,但如果第一次提交或重新提交更正,则不会发送它。我在MAIL函数中做错了什么,或者我的错误数组是否构建错误?谢谢你的建议。

这是我的PHP代码:

<?php

$firstnameErr = $lastnameErr = $Q1Err = $Q2Err = $Q3Err = $Q4Err = $Q5Err = $Q6Err = $Q7Err = $Q8Err = $Q9Err = $Q10Err = $Q11Err = $A1Err = $A2Err = $A3Err = $A4Err = $A5Err = $A6Err = $A7Err = $A8Err = $A9Err = $A10Err = $A11Err = "";
$firstname = $lastname = $Q1 = $Q2 = $Q3 = $Q4 = $Q5 = $Q6 = $Q7 = $Q8 = $Q9 = $Q10 = $Q11 = $A1 = $A2 = $A3 = $A4 = $A5 = $A6 = $A7 = $A8 = $A9 = $A10 = $A11 = "";


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

//Here are the fields it is checking

    $firstname = $_POST["firstname"];
    $lastname = $_POST["lastname"];
    $Q1 = $_POST["Q1"];
    $Q2 = $_POST["Q2"];
    $Q3 = $_POST["Q3"];
    $Q4 = $_POST["Q4"];
    $Q5 = $_POST["Q5"];
    $Q6 = $_POST["Q6"];
    $Q7 = $_POST["Q7"];
    $Q8 = $_POST["Q8"];
    $Q9 = $_POST["Q9"];
    $Q10 = $_POST["Q10"];
    $Q11 = $_POST["Q11"];
    $A1 = $_POST["A1"];
    $A2 = $_POST["A2"];
    $A3 = $_POST["A3"];
    $A4 = $_POST["A4"];
    $A5 = $_POST["A5"];
    $A6 = $_POST["A6"];
    $A7 = $_POST["A7"];
    $A8 = $_POST["A8"];
    $A9 = $_POST["A9"];
    $A10 = $_POST["A10"];
    $A11 = $_POST["A11"];


//Start of First Name Validation

    if (empty($_POST["firstname"])) {   //NOTE: Below is where it will post the error message
        $firstnameErr = "Your first name is required please";
   } 
   else {
        $firstname = $_POST["firstname"];
   }

//Start of Last Name Validation

    if (empty($_POST["lastname"])) {
        $lastnameErr = "Your last name is required please";
  } 
  else {
        $lastname = $_POST["lastname"];
  }

//Start of Question 1 Radio Button Validation

    if (empty($_POST["Q1"])) {
        $Q1Err = "Please select an answer";
  } 
  else {
        $Q1 = $_POST["Q1"];
  }

//Start of Answer 2 - not actually validating

    if (empty($_POST["A1"])) {
        $A1 = "";
  } 
  else {
        $A1 = $_POST["A1"];
  }

//Start of Question 2 Radio Button Validation

    if (empty($_POST["Q2"])) {
        $Q2Err = "Please select an answer";
  } 
  else {
        $Q2 = $_POST["Q1"];
  }

    if (empty($_POST["A2"])) {
        $A2 = "";
  } 
  else {
        $A2 = $_POST["A2"];
  }  

    if (empty($_POST["Q3"])) {
        $Q3Err = "Please select an answer";
  } 
  else {
        $Q3 = $_POST["Q3"];
  }

    if (empty($_POST["A3"])) {
        $A3 = "";
  } 
  else {
        $A3 = $_POST["A3"];
  }

    if (empty($_POST["Q4"])) {
        $Q4Err = "Please select an answer";
  } 
  else {
        $Q4 = $_POST["Q4"];
  }
    if (empty($_POST["A4"])) {
        $A4 = "";
  } 
  else {
        $A4 = check_input($_POST["A4"]);
  }   

    if (empty($_POST["Q5"])) {
        $Q5Err = "Please select an answer";
  } 
  else {
        $Q5 = $_POST["Q5"];
  }

    if (empty($_POST["A5"])) {
        $A5 = "";
  } 
  else {
        $A5 = $_POST["A5"];
  }

    if (empty($_POST["Q6"])) {
        $Q6Err = "Please select an answer";
  } 
  else {
        $Q6 = $_POST["Q6"];
  }

    if (empty($_POST["A6"])) {
        $A6 = "";
  } 
  else {
        $A6 = $_POST["A6"];
  } 

    if (empty($_POST["Q7"])) {
        $Q7Err = "Please select an answer";
  } 
  else {
        $Q7 = $_POST["Q7"];
  }
    if (empty($_POST["A7"])) {
        $A7 = "";
  } 
  else {
    $A7 = $_POST["A7"];
  }

    if (empty($_POST["Q8"])) {
        $Q8Err = "Please select an answer";
  } 
  else {
        $Q8 = $_POST["Q8"];
  }

    if (empty($_POST["A8"])) {
        $A8 = "";
  } 
  else {
        $A8 = $_POST["A8"];
  }   

    if (empty($_POST["Q9"])) {
        $Q9Err = "Please select an answer";
  } 
 else {
        $Q9 = $_POST["Q9"];
  }

    if (empty($_POST["A9"])) {
        $A9 = "";
  } 
  else {
        $A9 = $_POST["A9"];
  }

    if (empty($_POST["Q10"])) {
        $Q10Err = "Please select an answer";
  } 
  else {
        $Q10 = $_POST["Q10"];
  }

    if (empty($_POST["A10"])) {
        $A10 = "";
  } 
  else {
        $A10 = $_POST["A10"];
  } 

    if (empty($_POST["Q11"])) {
        $Q11Err = "Please select an answer";
  } 
  else {
        $Q11 = $_POST["Q11"];
  }

    if (empty($_POST["A11"])) {
        $A11 = "";
  } 
  else {
        $A11 = $_POST["A11"]; 
}

//Email out results code

        if ($firstnameErr == "" && $lastnameErr == "" && $Q1Err == "" && $Q2Err == ""&& $Q3Err == "" && $Q4Err == "" && $Q5Err == "" && $Q6Err == "" && $Q7Err == "" && $Q8Err == "" && $Q9Err == "" && $Q10Err == "" && $Q11Err == "")  { 

/* Send the message using mail() function */
        mail($myemail, $subject, $message);{
        $myemail = "anyone@gmail.com";
        $subject = "Exit Survey Completed";
        $message = "Greetings! The Exit Survey you sent out has been completed by: 

        First Name: $firstname 
        Last Name: $lastname

        Here is what $firstname had to say:

        1: Do you like beans? 
        Answer: $Q1
        Explaination, if recorded: $A1

        2. Do you like George Wendt?
        Answer: $Q2
        Explaination, if recorded: $A2

        3. Would you like to watch a movie about beans?
        Answer: $Q3
        Explaination, if recorded: $A3

        4. Would you watch a movie where George Wendt eats beans?
        Answer: $Q4
        Explaination, if recorded: $A4

        5. ___ ?
        Answer: $Q5
        Explaination, if recorded: $A5

        6. ___ ?
        Answer: $Q6
        Explaination, if recorded: $A6

        7. ___ ? 
        Answer: $Q7
        Explaination, if recorded: $A7

        8. ___ ? 
        Answer: $Q8
        Explaination, if recorded: $A8

        9. Did you feel you received ongoing feedback on your performance?  
        Answer: $Q9
        Explaination, if recorded: $A9

        10. Did you feel that the feedback on your performance was presented to you in a 
        sensitive and confidential manner?  
        Answer: $Q10
        Explaination, if recorded: $A10

        11. ___?
        Answer: $Q11
        Explaination, if recorded: $A11

        Additional Comments by $firstname :
        $comments

        Have a great day!
______________________________________________________________________________


Thank you";

/* Redirect visitor to the thank you page */
header('Location: thanks.htm');
exit();

        }
  }
?>

1 个答案:

答案 0 :(得分:3)

在设置您尝试使用的值之前,您已拨打mail()。反过来。

$myemail = "anyone@gmail.com";
$subject = "Exit Survey Completed";
$message = "Greetings! The Exit Survey you sent out has been completed by: 
           ...
Thank you";

mail($myemail, $subject, $message);