验证后发送详细信息到电子邮件

时间:2013-12-06 20:40:08

标签: php forms validation

我已经在联系表单中添加了用于验证信息的php代码.....但我不知道如何允许它在验证后发送详细信息....这里是代码......验证后应添加哪些代码,以便将详细信息发送到我的电子邮件

    <?php
// define variables and set to empty values
$first_nameErr = $emailErr = $genderErr = $prefixErr = $messageErr = $last_nameErr =                       $confirm_emailErr = $ic_passportErr = $addressErr = $cityErr = $stateErr = $postal_codeErr = $mobile_numberErr = $licenceErr = $subjectErr = "";
$first_name = $email = $gender = $prefix = $message = $last_name = $confirm_email = $ic_passport = $address = $city = $state = $postal_code = $mobile_number = $licence = $subject = "";

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

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

    if (empty($_POST["confirm_email"]))
    {$confirm_emailErr = "E-mail is required";}
  else
    {
    $confirm_email = test_input($_POST["confirm_email"]);
   // check if e-mail address syntax is valid
    if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$confirm_email))
      {
      $confirm_emailErr = "Invalid e-mail "; 
      }
    }

   if (empty($_POST["ic_passport"]))
     {$ic_passportErr = "Ic / Passport No. is required";}
    else
     {
       $ic_passport = test_input($_POST["ic_passport"]);
     // check if ic/passport is valid 
     if (!preg_match("/^[a-zA-Z0-9 ]*$/",$ic_passport))
      {
       $ic_passportErr = "Invalid Ic / Passport No."; 
       }
      }

   if (empty($_POST["message"]))
    {$messageErr = "Please type your message on the space provided above";}
      else
     {$message = test_input($_POST["message"]);}

   if (empty($_POST["subject"]))
     {$subjectErr = "Subject is required";}
   else
    {$subject = test_input($_POST["subject"]);}

   if (empty($_POST["gender"]))
     {$genderErr = "Gender is required";}
   else
    {$gender = test_input($_POST["gender"]);}

  if (empty($_POST["address"]))
    {$addressErr = "Address is required";}
   else
    {$address = test_input($_POST["address"]);}

    if (empty($_POST["city"]))
    {$cityErr = "City is required";}
    else
    {$city = test_input($_POST["city"]);}

     if (empty($_POST["state"]))
     {$stateErr = "State is required";}
    else
     {$state = test_input($_POST["state"]);}

      if (empty($_POST["postal_code"]))
     {$postal_codeErr = "Postal Code is required";}
    else
      {$postal_code = test_input($_POST["postal_code"]);}

      if (empty($_POST["mobile_number"]))
     {$mobile_numberErr = "Mobile Number is required";}
   else
     {$mobile_number = test_input($_POST["mobile_number"]);}

    if (empty($_POST["licence"]))
    {$licenceErr = "Please choose the package you would like to enquire";}
     else
     {$licence = test_input($_POST["licence"]);}
 }

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

2 个答案:

答案 0 :(得分:0)

使用mail()函数:

邮件(到,主题,消息,标题,参数)

http://www.w3schools.com/php/php_mail.asp

答案 1 :(得分:0)

将您的错误合并到变量或数组中 之后,检查if()语句中的错误是否为空 如果它成真,请使用mail()功能发送邮件。
这将使您确保填写所有必填字段并完成验证。然后可以通过邮件发送详细信息。

相关问题