用于电子邮件和错误处理的html表单到php

时间:2016-06-17 18:18:19

标签: javascript php html forms

我已经找到了这个问题的答案,很多都接近我要找的东西,但还没有雪茄!

我有一个html表单,提交时转到php。 php做了一些基本的错误检查/验证。如果一切顺利,那么它会发送一封电子邮件。如果出现问题,我会通过以下方式发送警告弹出窗口:

echo "<script type='text/javascript'>alert('$message');</script>";

我的问题是,当用户点击弹出窗口中的“确定”后,我想将用户返回到表单,并将他们在表单中输入的所有数据保留在表单中,这样他们就不会有重新输入。我尝试过使用标题刷新,但这会让我看到一个空白表单。我尝试了类似于以下命令的各种混合:

echo "<input class="field" type="text" name="first_name" value = "<?php echo $_POST['first_name']; ?>"/>";

但它永远不适合我。另外,如果这是正确的事情,那么我不知道确切地将它放在代码中的位置(在头部命令之前,在头部之后但在javascript警报之前等)我也遇到了问题。线的语法和编码。

非常感谢能提供的任何帮助。

这是php文件。功能死亡是我试图让它为我工作的地方。

<?php
if(isset($_POST['email'])) {

    // CHANGE THE TWO LINES BELOW
    $email_to = "me@test.com";

    $email_subject = "form submission";

    function died($error) {
        // your error code can go here
        $message = "Sorry, but there are errors found with the form you submitted.\\n";
        $message .= $error. " \\n";
        $message .= "Please go back and fix these errors. \\n";
        header("Refresh: 0; url=contact-us.html");

        echo "<script type='text/javascript'>alert('$message');</script>";

        die();
    }

    // validation expected data exists
    if(!isset($_POST['first_name']) ||
        !isset($_POST['last_name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['telephone']) ||
        !isset($_POST['comments'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $first_name = $_POST['first_name']; // required
    $last_name = $_POST['last_name']; // required
    $email_from = $_POST['email']; // required
    $telephone = $_POST['telephone']; // not required
    $comments = $_POST['comments']; // required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'The Email Address you entered does not appear to be valid.\\n';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
    $error_message .= 'The First Name you entered does not appear to be valid.\\n';
  }
  if(!preg_match($string_exp,$last_name)) {
    $error_message .= 'The Last Name you entered does not appear to be valid.\\n';
  }
  if(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.\\n';
  }
  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Form details below.\n\n";

    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "First Name: ".clean_string($first_name)."\n";
    $email_message .= "Last Name: ".clean_string($last_name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Telephone: ".clean_string($telephone)."\n";
    $email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  

header('Location: /contactresponse.html');
?>


<?php
}
die();
?>

0 个答案:

没有答案