联系表单未显示错误消息或发送电子邮件

时间:2017-03-23 20:13:06

标签: php html forms email

在我进入之前,我想说这确实是从本地服务器发送电子邮件,所以我已经设置了所有内容。添加此表单验证后,它不再发送电子邮件或显示错误。它只是刷新页面。我是php编码的新手,所以我确定我的if语句错误或类似。

HTML

<section id="contact">
            <h1 class="section-header">Contact Us Directly</h1>
            <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4>
            <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                <label for="fname">First Name</label>
                <input type="text" id="fname" name="firstname" value="<?php $firstname ?>" placeholder="Your name.." tabindex="1" autofocus>
                <span class="error"><?php $firstname_error ?></span>
                <label for="lname">Last Name</label>
                <input type="text" id="lname" name="lastname" value="<?php $lastname ?>" placeholder="Your last name.." tabindex="2">
                <span class="error"><?php $lastname_error ?></span>
                <label for="email">Email</label>
                <input type="text" id="email" name="email" value="<?php $email ?>" placeholder="Your email.." tabindex="3">
                <span class="error"><?php $email_error ?></span>
                <label for="message">Message</label>
                <textarea id="subject" name="message" value="<?php $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea>
                <span class="error"><?php $message_error ?></span>
                <input type="submit" value="Submit" tabindex="5"> 
                <span class="success"><?php $success ?></span>
            </form>
        </section>

PHP

<?php

$firstname_error = $lastname_error = $email_error = $message_error = "";
$firstname = $lastname = $email = $message = $success = "";

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

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

  if (empty($_POST["email"])) {
    $email_error = "Email is required";
  } else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
      $email_error = "Invalid email format"; 
    }
  }

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


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){
      $message_body = '';
      unset($_POST['submit']);
      foreach ($_POST as $key => $value){
          $message_body .=  "$key: $value\n";
      }

      $EmailFrom = localhost;
      $EmailTo = "testrepairmail69@gmail.com";
      $Subject = "New Order From tabletscreenfixer.com";
      if (mail($EmailTo, $Subject, $message)){
          $success = "Message sent, thank you for contacting us!";
          $firstname = $lastname = $email = $message = '';
      }
  }

}



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

?>

2 个答案:

答案 0 :(得分:2)

你的守则正在乞求一些改进,正如@R史密斯提到的那样;然而这个版本有效;我在我的电脑上测试过。 check the image

 <?php

  $firstname_error = $lastname_error = $email_error = $message_error = "";
  $firstname = $lastname = $email = $message = $success = "";

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

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

if (empty($_POST["email"])) {
    $email_error = "Email is required";
} else {
    $email = test_input($_POST["email"]);
    // check if e-mail address is well-formed
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $email_error = "Invalid email format";
    }
}

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


if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){
    $message_body = '';
    unset($_POST['submit']);
   // var_dump($_POST); exit();
    foreach ($_POST as $key => $value){
        $message_body .=  "$key: $value\n";
    }
    $EmailFrom = "test@gamil.com";
    $EmailTo = "tabletscreenfixer.com";//-> the message will be sent to this address if you have configure mail stuff well
    $Subject = "New Order From tabletscreenfixer.com";
    if (mail($EmailTo, $Subject, $message)){
        $success = "Message sent, thank you for contacting us!";
        $firstname = $lastname = $email = $message = '';
    }else{
        echo "Failure";
    }
 }else{
    echo "Failure 2";
   }
 }
    function test_input($data) {
       $data = trim($data);
       $data = stripslashes($data);
       $data = htmlspecialchars($data);
       return $data;
    }
 ?>

<html>
<head>
    <title>Test</title>
</head>
<body>
<section id="contact">
    <h1 class="section-header">Contact Us Directly</h1>
    <h4 class="text-center">Have any quesitons not answered in the <span><a href="questions.php">Questions Page</a></span>.</h4>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
        <label for="fname">First Name</label>
        <input type="text" id="fname" name="firstname" value="<?php echo $firstname ?>" placeholder="Your name.." tabindex="1" autofocus>
        <span class="error"><?php echo $firstname_error ?></span>
        <label for="lname">Last Name</label>
        <input type="text" id="lname" name="lastname" value="<?php echo $lastname ?>" placeholder="Your last name.." tabindex="2">
        <span class="error"><?php echo $lastname_error ?></span>
        <label for="email">Email</label>
        <input type="text" id="email" name="email" value="<?php echo $email ?>" placeholder="Your email.." tabindex="3">
        <span class="error"><?php echo $email_error ?></span>
        <label for="message">Message</label>
        <textarea id="subject" name="message" value="<?php echo $message ?>" placeholder="Write something.." style="height:200px" tabindex="4"> </textarea>
        <span class="error"><?php echo $message_error ?></span>
        <input type="submit" value="Submit" tabindex="5">
        <span class="success"><?php $success ?></span>
    </form>
</section>
</body>
</html>

编辑:输入值属性中缺少回显; 希望它有所帮助;

答案 1 :(得分:1)

添加一些调试。例如,在出现错误的任何地方,都会增加错误计数器。像这样:

if (empty($_POST["email"])) {
  $email_error = "Email is required";
  $errors++;
} else {
  $email = test_input($_POST["email"]);
  // check if e-mail address is well-formed
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    $email_error = "Invalid email format"; 
  }
}

然后,最后,而不是长条件:

if ($firstname_error == '' and $lastname_error == '' and $email_error == '' and $message_error == '' ){

您可以使用以下内容:

if ($errors == 0){

表示没有错误。然后,如果在尝试发送邮件时检查失败:

  if (mail($EmailTo, $Subject, $message)){
      $success = "Message sent, thank you for contacting us!";
      $firstname = $lastname = $email = $message = '';
  }
  else {
      echo "The mail command failed!!";
  }

最后,给自己一些指示,表明存在错误,仅适用于您的测试阶段。你可以删除其他(甚至添加更好的样式并保留它):

if ($errors == 0){

 // snip - lines of code in here removed to keep this snippet readable. this is your test and send mail logic.

}
else {
  echo "You've got $errors errors!  You need to correct them!";
}

通过这些更改,您应该能够快速找到问题所在。正如我所说,一旦你完成调试,你也可以删除一些代码(比如echo语句)。

祝你好运!