PHP-联系表单没有重定向

时间:2018-03-16 04:10:08

标签: php html web-deployment

我在单独的文件中有联系表格。它接收来自普通HTML表单的输入,验证输入是否有效,然后它发送带有输入到某个电子邮件地址的电子邮件。发送电子邮件后,它应该重定向到另一个页面,但它不会!

感谢您的帮助! :)

    <?php 

// define variables and set to empty values
$name_error = $email_error = $phone_error =  "";
$name = $email = $phone = $message = $success = "";

//form is submitted with POST method
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  if (empty($_POST["name"])) {
    $name_error = "Name is required";
  } else {
    $name = test_input($_POST["name"]);
    // check if name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
      $name_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["phone"])) {
    $phone_error = "Phone is required";
  } else {
    $phone = test_input($_POST["phone"]);
    // check if e-mail address is well-formed
    if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
      $phone_error = "Invalid phone number"; 
    }
  }



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

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

      $to = 'test@gmail.com';
      $subject = 'Test';
      if (mail($to, $subject, $message_body)){
          header("Location: test.php");
          $success = "Message sent, thank you for contacting us!";
          $name = $email = $phone = $message = $url = '';
      }
  }


}

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

2 个答案:

答案 0 :(得分:0)

我认为您的电子邮件无法正常运作。

您可以像这样更改代码。

if (mail($to, $subject, $message_body)) {
    $success = "Message sent, thank you for contacting us!";
    $name = $email = $phone = $message = $url = '';
    header("Location: test.php");
    exit;
}
else {
    $success = "Mail sending failed! Please try again.";
    $name = $email = $phone = $message = $url = '';

    //to show error use: echo error_get_last()['message']

    header("Location: test.php");
    exit;
}

最好在标题重定向后使用退出。

同时检查您的表单提交方法。是post还是get

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // your rest of the code
}
else {
    echo 'Invalid server request!';
}

同时检查此条件

if ($name_error == '' && $email_error == '' && $phone_error == '' ){
    //your code
}
else {
    echo $name_error;
    echo $email_error;
    ech $phone_error;
}

答案 1 :(得分:0)

试试这段代码:

<?php 
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: test@gmail.com' . "\r\n";
 if (mail($to, $subject, $message_body,$headers))
 echo "success";
else 
 echo ""failed:
?>

此邮件功能重定向工作正常。     您将在本地和时间一次检查邮件(SMTP)启用。在线服务器。