isset($ _ Post)没有从邮件表单中获取值

时间:2014-09-19 15:07:37

标签: php forms email post isset

所以我正在尝试将联系表单(contact.php)中的值发送到我的文件send_form_email.php。 出于某种原因,我没有从contact.php获取值。我非常感谢你的帮助!

这是我的contact.php文件中的表单代码:

<form role="form" id="contactform" name="contactform" enctype='multipart/form-data' method="post" action="send_form_email.php">
     <div class="form-group">
       <label for="inputFName" style="color: #ECC444; text-shadow: 2px 2px #414141;">First Name:</label>
       <input type="text" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">
     </div> 
     <div class="form-group">
       <label for="inputLName" style="color: #ECC444; text-shadow: 2px 2px #414141;">Last Name:</label>
       <input type="text" class="form-control" id="inputLName" placeholder="Enter Last Name" maxlength="50" size="30">
     </div> 
     <div class="form-group">
       <label for="inputEmail" style="color: #ECC444; text-shadow: 2px 2px #414141;">Email Address:</label>
       <input type="text" class="form-control" id="inputEmail" placeholder="Enter Email" maxlength="80" size="30">
     </div>
     <div class="form-group">
       <label for="inputComments" style="color: #ECC444; text-shadow: 2px 2px #414141;">Comments:</label>
       <textarea class="form-control"name="inputComments" rows="3" maxlength="1000" cols="25" rows="6"></textarea>
     </div>
     <?php
       require_once('recaptchalib.php');
       $publickey = "<public-key>"; 
       echo recaptcha_get_html($publickey);
     ?>
  <br/>
  <button type="button" class="btn btn-default" onclick="sendEmail()">Submit</button>
</form>

单击“提交”按钮后,将调用以下Javascript函数:

<script language="javascript">
    function sendEmail() {
      document.forms["contactform"].submit();
    } 
</script>

提交表单并调用send_form_email.php文件:

<?php session_start();
require_once('recaptchalib.php');
  $privatekey = "privatekey";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);

  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } 
  else {

    if(isset($_POST['inputEmail'])) {

      // EDIT THE 2 LINES BELOW AS REQUIRED
      $email_to = "example@example.com";
      $email_subject = "Example Contact Form";

      function died($error) {
          // your error code can go here
          echo "We are very sorry, but there were error(s) found with the form you submitted. ";
          echo "These errors appear below.<br /><br />";
          echo $error."<br /><br />";
          echo "Please go back and fix these errors.<br /><br />";
          die();
      }

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

      date_default_timezone_set('America/Montreal');

      $first_name = $_POST['inputFName']; // required
      $last_name = $_POST['inputLName']; // required
      $email_from = $_POST['inputEmail']; // required
      $comments = $_POST['inputComments']; // required
      $date = getdate();

      $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.<br />';
      }
        $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.<br />';
      }
      if(!preg_match($string_exp,$last_name)) {
        $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
      }
      if(strlen($comments) < 2) {
        $error_message .= 'The Comments you entered do not appear to be valid.<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }
      $email_message = "Form details submitted on ".$date." are as follows:\n\n";
      $email_message_sender = "Thank you for your contact request! We will be reply as soon as possible!";

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

      function clean_link($string) {
        $bad = array("./");
        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 .= "Comments: ".clean_string($comments)."\n";

      $email_message_sender .= "First Name: ".clean_string($first_name)."\n";
      $email_message_sender .= "Last Name: ".clean_string($last_name)."\n";
      $email_message_sender .= "Email: ".clean_string($email_from)."\n";
      $email_message_sender .= "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);  

      $headers2 = 'From: '.$email_from."\r\n".
      'Reply-To: '.$email_from."\r\n" .
      'X-Mailer: PHP/' . phpversion();
      @mail($email_from, $email_subject, $email_message_sender, $headers2);  
  ?>
  <!-- include your own success html here -->

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

  <?php
  }
}
?>

由于显而易见的原因,我遗漏了某些值,例如recrapcha的公钥/私钥和我的电子邮件。

我无法弄清楚我做错了什么。

谢谢你们。

1 个答案:

答案 0 :(得分:3)

您的所有表单字段都缺少name属性。没有它,它们的值发送到服务器。

例如:

<input type="text" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">

缺少name="inputFName"

<input type="text" name="inputFName" class="form-control" id="inputFName" placeholder="Enter First Name" maxlength="50" size="30">