只有在按下提交按钮并且表单有效后,才能尝试用PHP发送电子邮件


Trying to send an email in PHP only after the submit button is pressed and the form is valid

我是PHP的新手,目前正在返回HTML。我已经制作了一个表格,并通过PHP发送和验证了数据,但只有在数据经过验证且正确后,我才会尝试将电子邮件发送给自己。目前,如果页面被加载,我认为它会发送一封电子邮件,每当我点击提交时,它就会发送,而数据不正确。

以下是我验证数据的地方:

<?php
  //Set main variables for the data.
  $fname = $lname = $email = $subject = $website = $likedsite = $findoption = $comments = "";
  //Set the empty error variables.
  $fnameErr = $lnameErr = $emailErr = $subjectErr = $commentsErr = $websiteErr = $findoptionErr = "";
  //Check to see if the form was submitted.
  if ($_SERVER["REQUEST_METHOD"] == "POST")
  {
    //Check the 'First Name' field.
    if (empty($_POST["fname"]))
    {
      $fnameErr = "First Name is Required.";
    } 
    else
    {
      $fname = validate_info($_POST["fname"]);
    }
    //Check the 'Last Name' field.
    if (empty($_POST["lname"]))
    {
      $lnameErr = "Last Name is Required.";
    } 
    else
    {
      $lname = validate_info($_POST["lname"]);
    }
    //Check the 'E-Mail' field.
    if (empty($_POST["email"]))
    {
      $emailErr = "E-Mail is Required.";
    } 
    else
    {
      $email = validate_info($_POST["email"]);
      //Check if valid email.
      if (!filter_var($email, FILTER_VALIDATE_EMAIL))
      {
        $emailErr = "Invalid E-Mail Format.";
      }
    }
    //Check the 'Subject' field.
    if (empty($_POST["subject"]))
    {
      $subjectErr = "Subject is Required.";
    } 
    else
    {
      $subject = validate_info($_POST["subject"]);
    }
    //Check the 'Website' field.
    if (empty($_POST["siteurl"]))
    {
      $website = "";
    } 
    else
    {
      $website = validate_info($_POST["siteurl"]);
      //Check if valid URL.
      if (!preg_match("/'b(?:(?:https?|ftp):'/'/|www'.)[-a-z0-9+&@#'/%?=~_|!:,.;]*[-a-z0-9+&@#'/%=~_|]/i",$website))
      {
        $websiteErr = "Invalid URL.";
      }
    }
    //Check the 'How Did You Find Us' options.
    if (empty($_POST["howfind"]))
    {
      $findoptionErr = "Please Pick One.";
    } 
    else
    {
      $findoption = validate_info($_POST["howfind"]);
    }
    //Check the comment box.
    if (empty($_POST["questioncomments"]))
    {
      $commentsErr = "Questions/Comments are Required.";
    } 
    else
    {
      $comments = validate_info($_POST["questioncomments"]);
    }
    //Pass any un-required data.
    $likedsite = validate_info($_POST["likedsite"]);
  }
  function validate_info($data)
  {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
?>

抱歉有点长。

这是我尝试发送电子邮件的地方。我试过两次不同的尝试,结果都是一样的。

    <?php
if (!empty($fnameErr) || !empty($lnameErr) || !empty($subjectErr) || !empty($emailErr) || !empty($commentErr) || !empty($websiteErr) || !empty($findoptionErr))
{
  echo "Sent!!";
}else
{
  echo"Not Sent!!";
}
  //Make the message.
  $message =
  "
  First Name: $fname.'n
  Last Name: $lname.'n
  Website: $website'n
  Did They Like the Site? $likedsite.'n
  How They Found Us. $findoption.'n
  Question/Comments:'n
  $comments.
  ";
  $message = wordwrap($message, 70);
  $headers = "From: $email";
  mail("me@gmail.com", $subject, $message, $headers);
?>

再次为长度感到抱歉。提前感谢也很抱歉,如果这是一个双重问题或描述不够,我也是新的堆栈溢出。

请尝试:

<?php
  //Set main variables for the data.
  $fname = $lname = $email = $subject = $website = $likedsite = $findoption = $comments = "";
  //Set the empty error variables.
  $fnameErr = $lnameErr = $emailErr = $subjectErr = $commentsErr = $websiteErr = $findoptionErr = "";
  //Initialize variable used to identify form is valid OR not. 
  $formValid = true;
  //Check to see if the form was submitted.
  if ($_SERVER["REQUEST_METHOD"] == "POST")
  {
    //Check the 'First Name' field.
    if (empty($_POST["fname"]))
    {
      $formValid = false;//Form not validate
      $fnameErr = "First Name is Required.";
    } 
    else
    {
      $fname = validate_info($_POST["fname"]);
    }
    //Check the 'Last Name' field.
    if (empty($_POST["lname"]))
    {
      $formValid = false;//Form not validate
      $lnameErr = "Last Name is Required.";
    } 
    else
    {
      $lname = validate_info($_POST["lname"]);
    }
    //Check the 'E-Mail' field.
    if (empty($_POST["email"]))
    {
      $formValid = false;//Form not validate
      $emailErr = "E-Mail is Required.";
    } 
    else
    {
      $email = validate_info($_POST["email"]);
      //Check if valid email.
      if (!filter_var($email, FILTER_VALIDATE_EMAIL))
      {
        $formValid = false;//Form not validate
        $emailErr = "Invalid E-Mail Format.";
      }
    }
    //Check the 'Subject' field.
    if (empty($_POST["subject"]))
    {
      $formValid = false;//Form not validate
      $subjectErr = "Subject is Required.";
    } 
    else
    {
      $subject = validate_info($_POST["subject"]);
    }
    //Check the 'Website' field.
    if (empty($_POST["siteurl"]))
    {
      $website = "";
    } 
    else
    {
      $website = validate_info($_POST["siteurl"]);
      //Check if valid URL.
      if (!preg_match("/'b(?:(?:https?|ftp):'/'/|www'.)[-a-z0-9+&@#'/%?=~_|!:,.;]*[-a-z0-9+&@#'/%=~_|]/i",$website))
      {
        $formValid = false;//Form not validate
        $websiteErr = "Invalid URL.";
      }
    }
    //Check the 'How Did You Find Us' options.
    if (empty($_POST["howfind"]))
    {
      $formValid = false;//Form not validate
      $findoptionErr = "Please Pick One.";
    } 
    else
    {
      $findoption = validate_info($_POST["howfind"]);
    }
    //Check the comment box.
    if (empty($_POST["questioncomments"]))
    {
      $formValid = false;//Form not validate
      $commentsErr = "Questions/Comments are Required.";
    } 
    else
    {
      $comments = validate_info($_POST["questioncomments"]);
    }
    //Pass any un-required data.
    $likedsite = validate_info($_POST["likedsite"]);
  }

  //If every variable value set, send mail OR display error...
  if (!$formValid){
     echo"Form not validate...";
  } 
  else {
      //Make the message.
      $message =
      "
      First Name: $fname.'n
      Last Name: $lname.'n
      Website: $website'n
      Did They Like the Site? $likedsite.'n
      How They Found Us. $findoption.'n
      Question/Comments:'n
      $comments.
      ";
      $message = wordwrap($message, 70);
      $headers = "From: $email";
      mail("me@gmail.com", $subject, $message, $headers);

      if($sendMail){
        echo "Mail Sent!!";
      }
      else {
        echo "Mail Not Sent!!";
      }
  }
  function validate_info($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
  }
?>

我根据一些变化修改我的答案。现在,只有当表单必填字段不为空并且根据您的验证,所有字段值都有效时,此代码才允许发送邮件。

如果有任何顾虑,请告诉我。!

根据我的设想,u是

  1. 尝试在if条件-应更改为AND时应用"OR",即将||更改为&amp;

  2. 您正在检查非空错误变量。。。其应当被改变以验证它们是否都是空的。

if (empty($fnameErr) && empty($lnameErr) && empty($subjectErr) && empty($emailErr) && empty($commentErr) && empty($websiteErr) && empty($findoptionErr)) { echo "sent"; }

而不是编写冗长的条件。

  1. 将所有错误消息分配给单个变量,并将错误附加到该变量($errorMsg)。这样做可以避免冗长的if-else梯形图
  2. empty($_POST["email"])更改为!isset($_POST["email"])-在所有语句中

然后将条件更新为以下

<?php
if($errorMsg == ''){
  //Make the message.
  $message ="
  First Name: ".$fname.".'n
  Last Name: ".$lname."'n
  Website: ".$website."'n
  Did They Like the Site? ".$likedsite."'n
  How They Found Us. ".$findoption."'n
  Question/Comments:'n
  ".$comments." ";
  $message = wordwrap($message, 70);
  $headers = "From: $email";
  mail("me@gmail.com", $subject, $message, $headers);
}else{
  // Show $errorMsg
}
?>

简单一点,我希望这能有所帮助。