PHP / AJAX电子邮件表单不会提交。

时间:2013-09-09 18:34:06

标签: php ajax email

我正在尝试设置一个相当直接的电子邮件表单,如果电子邮件成功提交将显示div,并且在字段未正确填写时显示错误消息。目前,Jquery方面的工作正常,但它不会发送电子邮件。我是PHP的新手,所以我不确定如何解决PHP方面的问题,但所有代码看起来都是正确的。

HTML:

<form id="contactForm" >
  <fieldset class="half float-left left">
    <div class="overlay-field">
      <label for="firstname" class="float-left">First Name</label>
      <input id="firstname" type="text" name="firstname" >
      <span id="errorFirstName" class="formError"></span>
    </div>  
    <div class="overlay-field" data-error="">
      <label for="telephone" class="float-left">Telephone</label>
      <input id="telephone" type="text" name="telephone" >
      <span id="errorPhone" class="formError"></span>
    </div>
  </fieldset>
  <fieldset class="half float-right right">
    <div class="overlay-field">
      <label for="lastname" class="float-left">Last Name</label>
      <input id="lastname" type="text" name="lastname" >
      <span id="errorLastName" class="formError"></span>
    </div>

    div class="overlay-field" data-error="">
      <label for="email" class="float-left">Email Address</label>
      <input id="email" type="text" name="email" >
      <span id="errorEmail" class="formError"></span>
    </div>
  </fieldset>
  <label for="message" class="textarea-label float-left">Your Message</label>
  <textarea id="message" value="Scope of Work" class="description" name="message" ></textarea>
  <span id="errorMessage" class="formError"></span>
  <div class="message_area">
    <p class="success_message">WOOT WOOT! You're message has been sent! We will get back to you shortly.</p>
    <p class="error_message">Something went wrong. Please make sure all fields are filled in properly.</p>
  </div>
  <button type="submit" id="submit" class="bttn bttn-4 bttn-4a">Send Message</button>
</form>

JQUERY:

   $(document).ready(function() {
    $('#contactForm #submit').click(function() {

      $('#contactForm .formError').html('');
      var isFocus=0;
      var isError=0; 

      var firstname=$('#contactForm #firstname').val();
      var lastname=$('#contactForm #lastname').val();
      var email=$('#contactForm #email').val();
      var telephone=$('#contactForm #telephone').val();
      var message=$('#contactForm #message').val();

      if(firstname=='') {
        $('#contactForm #errorFirstName').html('This is a required field.');
        $('#contactForm #firstname').focus();
        isFocus=1;
        isError=1;
      }
      if(lastname=='') {
        $('#contactForm #errorLastName').html('This is a required field.');
        $('#contactForm #lastname').focus();
        isFocus=1;
        isError=1;
      }
      if(telephone=='') {
        $('#contactForm #errorPhone').html('This is a required field.');
        $('#contactForm #telephone').focus();
        isFocus=1;
        isError=1;
      }
      if(email=='') {
        $('#contactForm #errorEmail').html('This is a required field.');
        if(isFocus==0) {
            $('#contactForm #email').focus();
            isFocus=1;
        }
        isError=1;
      } else {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(email)==false) {
            $('#contactForm #errorEmail').html('Invalid email address.');
            if(isFocus==0) {
                $('#contactForm #email').focus();
                isFocus=1;
            }
            isError=1;
        }
      }
      if(message=='') {
        $('#contactForm #errorMessage').html('This is a required field.');
        $('#contactForm #message').focus();
        isFocus=1;
        isError=1;
      }
      // Terminate the script if an error is found
      if(isError==1) {
        return false;
      }
      $.ajaxSetup ({
        cache: false
      });
      $.ajax({
        type: "POST",
        url: "/wp-content/themes/apds/contact-ajax.php",
        data: $("#contactForm").serializeArray(),
        success: function(msg) {
                $(".success_message").fadeIn().delay(5000).fadeOut();
                $("#contactForm")[0].reset();
                $('#contactForm #submit').removeAttr("disabled");
        },
        error: function(ob,errStr) {
                $('#contactForm #submit').removeAttr("disabled");
        }
      });
      return false;
      });
    });

PHP

    <?php
     if(empty($_POST['firstname']) || 
     empty($_POST['lastname']) || 
     empty($_POST['telephone']) || 
     empty($_POST['email']) || 
     empty($_POST['message'])) {
       die('Error: Missing variables');
    }

    $email_subject = "Your email subject line";
    $firstname=$_POST['firstname'];
    $lastname=$_POST['lastname'];
    $email=$_POST['email'];
    $telephone=$_POST['telephone'];
    $message=$_POST['message'];
    $to='my@email.com';

    $headers = 'From: '.$email."\r\n" .
    'Reply-To: '.$email."\r\n" .
    'X-Mailer: PHP/' . phpversion();

    $email_subject = $email_subject;
    $body='You have got a new message from the contact form on your website.'."\n\n";
    $body.='First Name: '.$firstname."\n";
    $body.='Last Name: '.$lastname."\n";
    $body.='Email: '.$email."\n";
    $body.='Telephone: '.$telephone."\n";
    $body.='Message: '."\n".$message."\n";
    if(mail($to, $email_subject, $body, $headers)) {
die('Mail sent');
     } else {
die('Error: Mail failed');
    }
    ?>

0 个答案:

没有答案