PHP联系表单错误

时间:2017-01-17 16:16:17

标签: javascript php html phpmailer contact-form

我有一个使用html的联系表格,我也有一个sendEmail.php文件。 一切都在前端工作,并通知电子邮件已发送,但它似乎永远不会到我的收件箱。 我无法确定为什么会这样? 非常感谢帮助。

提前致谢。

这是HTML                 

           <div class="form-field">
                   <input name="contactName" type="text" id="contactName" placeholder="Name" value="" minlength="2" required="">
           </div>

           <div class="row">
                <div class="col-six tab-full">
                    <div class="form-field">
                        <input name="contactEmail" type="email" id="contactEmail" placeholder="Email" value="" required="">
                    </div>                         
                </div>
                <div class="col-six tab-full">              
                    <div class="form-field">
                        <input name="contactSubject" type="text" id="contactSubject" placeholder="Subject" value="">
                  </div>                               
                </div>
           </div>

           <div class="form-field">
                <textarea name="contactMessage" id="contactMessage" placeholder="message" rows="10" cols="50" required=""></textarea>
            </div> 

           <div class="form-field">
              <button class="submitform">Submit</button>

              <div id="submit-loader">
                 <div class="text-loader">Sending...</div>                             
                  <div class="s-loader">
                            <div class="bounce1"></div>
                            <div class="bounce2"></div>
                            <div class="bounce3"></div>
                        </div>
                    </div>
           </div>

        </form> 

        <!-- contact-warning -->
        <div id="message-warning"></div> 

        <!-- contact-success -->
        <div id="message-success">
           <i class="fa fa-check"></i>Your message was sent, thank you!<br>
        </div>

这是sendEmail.php     

$siteOwnersEmail = 'info@jalina-digital.co.uk';


if($_POST) {

$name = trim(stripslashes($_POST['contactName']));
$email = trim(stripslashes($_POST['contactEmail']));
$subject = trim(stripslashes($_POST['contactSubject']));
$contact_message = trim(stripslashes($_POST['contactMessage']));

// Check Name
if (strlen($name) < 2) {
    $error['name'] = "Please enter your name.";
}
// Check Email
if (!preg_match('/^[a-z0-9&\'\.\-_\+]+@[a-z0-9\-]+\.([a-z0-9\-]+\.)*+[a-z]{2}/is', $email)) {
    $error['email'] = "Please enter a valid email address.";
}
// Check Message
if (strlen($contact_message) < 15) {
    $error['message'] = "Please enter your message. It should have at least 15 characters.";
}
 // Subject
if ($subject == '') { $subject = "Contact Form Submission"; }


// Set Message
$message .= "Email from: " . $name . "<br />";
$message .= "Email address: " . $email . "<br />";
$message .= "Message: <br />";
$message .= $contact_message;
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";

// Set From: header
$from =  $name . " <" . $email . ">";

// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";


 if (!$error) {

  ini_set("sendmail_from", $siteOwnersEmail); // for windows server
  $mail = mail($siteOwnersEmail, $subject, $message, $headers);

    if ($mail) { echo "OK"; }
  else { echo "Something went wrong. Please try again."; }

} # end if - no validation error

else {

    $response = (isset($error['name'])) ? $error['name'] . "<br /> \n" : null;
    $response .= (isset($error['email'])) ? $error['email'] . "<br /> \n" : null;
    $response .= (isset($error['message'])) ? $error['message'] . "<br />" : null;

    echo $response;

 } # end if - there was a validation error

 }

?>

这里有一些JS。          var ssContactForm = function(){

/* local validation */      
    $('#contactForm').validate({

        /* submit via ajax */
        submitHandler: function(form) {             
            var sLoader = $('#submit-loader');          

            $.ajax({    
              type: "POST",
              url: "inc/sendEmail.php",
              data: $(form).serialize(),

              beforeSend: function() { 
                sLoader.fadeIn(); 
              },
              success: function(msg) {
                // Message was sent
                if (msg == 'OK') {
                    sLoader.fadeOut(); 
                   $('#message-warning').hide();
                   $('#contactForm').fadeOut();
                   $('#message-success').fadeIn();   
                }
                // There was an error
                else {
                    sLoader.fadeOut(); 
                   $('#message-warning').html(msg);
                    $('#message-warning').fadeIn();
                }
              },
              error: function() {
                sLoader.fadeOut(); 
                $('#message-warning').html("Something went wrong. Please try again.");
                 $('#message-warning').fadeIn();
              }
          });           
        }

    });
 }; 

1 个答案:

答案 0 :(得分:0)

您编写的代码有一些错误。

1)在HTML表单中显示<div class="row"> <div class="col-md-8 leftSide"> <div class="row"> <div class="col-md-12"> </div> </div> <div class="row"> <div class="col-md-12"> </div> </div> </div> <div class="col-md-4 rightSide"> <div class="row"> <div class="col-md-12"> </div> </div> </div> </div> ,您必须只是该输入示例的名称<button class="submitform">Submit</button>

2)在您的PHP sendEmail文件<input type="submit" name="send" class="submitform">Submit</input>必须变为if($_POST)

因此,这些更正将得到解决。另请注意在PHP中发送邮件不会立即收到消息,这可能需要一些时间。