PHP电子邮件错误警报消息

PHP电子邮件错误警报消息,php,forms,email,alert,Php,Forms,Email,Alert,所以我有一个死函数,我让它回显带有错误消息的警报。问题是,在我关闭该框后,代码将继续,并给出成功消息。如果我添加退出死亡或死亡()它将我重定向到一个白色页面。。。如果我把重定向头,它重定向之前,我可以看到警报框 下面是函数和代码 function died($error) { echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the

所以我有一个死函数,我让它回显带有错误消息的警报。问题是,在我关闭该框后,代码将继续,并给出成功消息。如果我添加
退出
死亡
死亡()它将我重定向到一个白色页面。。。如果我把重定向头,它重定向之前,我可以看到警报框

下面是函数和代码

 function died($error) {
  echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>';  // your error code can go here
}
<?php

if (isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "admin@domain.com";
  $email_subject = "NEW CLIENT!!!";

  function died($error) {
    echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>';  // your error code can go here
  }

  // validation expected data exists
  if(!isset($_POST['first_name']) ||
    !isset($_POST['email']) ||
    !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

  $email_from = $_POST['email']; // 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(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 .= "Email: ".clean_string($email_from)."\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);  

?>

<!---include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

}

?>  
函数失效($error){
echo“alert”(“非常抱歉,您提交的表单出现了错误。\n这些错误如下所示。\n\n“$error.”;;//您的错误代码可以转到此处
}

您的脚本不返回任何内容,因为未设置
$\u POST['email']
。所有代码都在条件块内


下面三行关闭原始的if语句,其中设置了if
$\u POST['email']
。此文件应位于其自己的文件中,以便提交表单。有一个单独的html文件,其中包含对此文件的操作。e、 g:

仅在未发现错误时发送邮件。成功信息也是如此。因此,在检查下面是否添加了错误>0时,需要额外添加一个ELSE

尝试将函数放在中间位置,而不是if语句中

检查电子邮件地址的正则表达式不正确。例如foo@bar.travel不匹配

<?php
   function clean_string($string) {
    $bad = array("content-type","bcc:","to:","cc:","href");
    return str_replace($bad,"",$string);
  }
  function died($error) {
    echo '<script type="text/javascript">alert("We are very sorry, but there were error(s) found with the form you submitted. \nThese errors appear below.\n\n' . $error . '");</script>';  // your error code can go here
  }


if (isset($_POST['email'])) {
  // EDIT THE 2 LINES BELOW AS REQUIRED
  $email_to = "admin@domain.com";
  $email_subject = "NEW CLIENT!!!";


  // validation expected data exists
  if(!isset($_POST['first_name']) ||
    !isset($_POST['email']) ||
    !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

  $email_from = $_POST['email']; // required

  $comments = $_POST['comments']; // required  

  $error_message = "";

// below regex used to be a bit valid years ago. not anymore
//      $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';


  if(!filter_input(INPUT_POST,'email', FILTER_VALIDATE_EMAIL)) {
    $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(strlen($comments) < 2) {
    $error_message .= 'The Comments you entered do not appear to be valid.\n';
  }

  if(strlen($error_message) > 0) {
    died($error_message);
  }
  else {
    $email_message = "Form details below.\n\n";


  $email_message .= "First Name: ".clean_string($first_name)."\n";

  $email_message .= "Email: ".clean_string($email_from)."\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);  

?>

<!---include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?php

  }
}
?>  

为什么要定义两次函数???这肯定是个错误我对这有点陌生。它的功能就像发送电子邮件一样。我想做一个if和else语句,但我不明白下面的3行在哪里结束了}。如能提供进一步指导,将不胜感激。