2014-01-14 137 views
0

我在做什麼錯在這裏聯繫表格不發送和電子郵件我收到一條錯誤消息「對不起,這次發生錯誤發送您的消息。」當我填寫表格,然後點擊發送PHP聯繫表格與JS驗證不發送電子郵件

的index.php

<head> 
    <title>Khaleda Rajab + Fahad Al Marzouq</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <!-- Bootstrap --> 
    <link href="css/bootstrap.min.css" rel="stylesheet"> 
    <link href="css/style.css" rel="stylesheet"> 
    <link href="css/flat-ui.css" rel="stylesheet"> 

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries --> 
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> 
    <!--[if lt IE 9]> 
     <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script> 
     <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script> 
    <![endif]--> 
    <script type="text/javascript"> 
    $window = $(window); 

$('section[data-type="background"]').each(function(){ 
    var $bgobj = $(this); // assigning the object 
    $(window).scroll(function() { 

     // Scroll the background at var speed 
     // the yPos is a negative value because we're scrolling it UP!        
     var yPos = -($window.scrollTop()/$bgobj.data('speed')); 

     // Put together our final background position 
     var coords = '50% '+ yPos + 'px'; 

     // Move the background 
     $bgobj.css({ backgroundPosition: coords }); 

    }); // window scroll Ends 

}); 
    </script> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> 
     <script type='text/javascript' src="js/validation.js"></script> 
    <script type="text/javascript"> 
var jump=function(e) 
{ 
     //prevent the "normal" behaviour which would be a "hard" jump 
     e.preventDefault(); 
     //Get the target 
     var target = $(this).attr("href"); 
     //perform animated scrolling 
     $('html,body').animate(
     { 
       //get top-position of target-element and set it as scroll target 
       scrollTop: $(target).offset().top 
     //scrolldelay: 2 seconds 
     },1000,function() 
     { 
       //attach the hash (#jumptarget) to the pageurl 
       location.hash = target; 
     }); 

} 

$(document).ready(function() 
{ 
     $('a[href*=#]').bind("click", jump); 
     return false; 
}); 
    </script> 

</head> 

<body> 

    <section id="home" data-speed="10" data-type="background"> 
     <img class="img-responsive" src="media/img/khaleda_rajab_plus_fahad_almarzouq_logo.png" alt="Khaleda Rajab + Fahad AlMarzouq"> 
     <p>The website is under construction.</p> 
     <nav> 
     <a href="#contact" class="btn btn-block btn-lg btn-primary">Contact Us</a> 
     </nav> 
     <p class="small"> 
     Powered by <a href="http://www.cinqomedia.com" target="_blank">Cinqo Media</a> 
     </p> 
</section> 
    <section id="contact" data-speed="6" data-type="background"> 
     <h1 id="contact">Contact Us</h1> 
       <div class="col-md-3"> 
      <div class="form-group"> 
     <form name="contactForm" class="form-horizontal" id='contact_form' method="post" action='email.php'> 
<div id='name_error' class='error'>Please enter your name.</div> 
<div> 
<input type='text' name='name' id='name' class="form-control" placeholder="Name" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Name'"> 
</div> 
<div id='email_error' class='error'>Please enter a valid E-mail address.</div> 
<div> 
<input type='text' name='email' id='email' class="form-control" placeholder="E-mail address" onfocus="this.placeholder = ''" onblur="this.placeholder = 'E-mail address'"> 
<div> 
<div id='subject_error' class='error'>Please enter the subject.</div> 
<div> 
<input type='text' name='subject' id='subject' class="form-control" placeholder="Subject" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Subject'"> 
</div> 
<div id='message_error' class='error'>Please enter your message.</div> 
<div> 
<textarea name='message' id='message' class="form-control" rows="8" placeholder="Message" onfocus="this.placeholder = ''" onblur="this.placeholder = 'Message'"></textarea> 
</div> 
<div id='mail_success' class='success'>Your message has been sent successfully.</div> 
<div id='mail_fail' class='error'>Sorry, error occured this time sending your message.</div> 
<input type='submit' id='send_message' class="btn btn-block btn-lg btn-primary" value='Send'> 
</form> 
       </div>   
      </div> 
     </section> 

    <script src="https://code.jquery.com/jquery.js"></script> 
    <script src="js/bootstrap.min.js"></script> 
    <script src="js/backstretch.min.js"></script> 
    <script type="text/javascript"> 
     $.backstretch([ 
     "media/img/cat_eye_mask_bg_1.png" 
    , "media/img/cat_eye_mask_bg_2.png" 
    , "media/img/cat_eye_mask_bg_3.png" 
    ], {duration: 4000, fade: 750}); 
    </script> 

</body> 

email.php

$subject = $_REQUEST['subject'] . ' Ajax HTML Contact Form : Demo'; 
$to = $_REQUEST['[email protected]']; //Recipient's E-mail 

$headers = 'MIME-Version: 1.0' . "rn"; 
$headers .= "From: " . $_REQUEST['email'] . "rn"; // Sender's E-mail 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; 

$message .= 'Name: ' . $_REQUEST['name'] . "<br>"; 
$message .= $_REQUEST['message']; 

if (@mail($to, $subject, $message, $headers)) 
{ 
// Transfer the value 'sent' to ajax function for showing success message. 
echo 'sent'; 
} 
else 
{ 
// Transfer the value 'failed' to ajax function for showing error message. 
echo 'failed'; 
} 

validation.js

$(document).ready(function(){ 
    $('#send_message').click(function(e){ 

     //Stop form submission & check the validation 
     e.preventDefault(); 

     // Variable declaration 
     var error = false; 
     var name = $('#name').val(); 
     var email = $('#email').val(); 
     var subject = $('#subject').val(); 
     var message = $('#message').val(); 

     // Form field validation 
     if(name.length == 0){ 
      var error = true; 
      $('#name_error').fadeIn(500); 
     }else{ 
      $('#name_error').fadeOut(500); 
     } 
     if(email.length == 0 || email.indexOf('@') == '-1'){ 
      var error = true; 
      $('#email_error').fadeIn(500); 
     }else{ 
      $('#email_error').fadeOut(500); 
     } 
     if(subject.length == 0){ 
      var error = true; 
      $('#subject_error').fadeIn(500); 
     }else{ 
      $('#subject_error').fadeOut(500); 
     } 
     if(message.length == 0){ 
      var error = true; 
      $('#message_error').fadeIn(500); 
     }else{ 
      $('#message_error').fadeOut(500); 
     } 

     // If there is no validation error, next to process the mail function 
     if(error == false){ 
      // Disable submit button just after the form processed 1st time successfully. 
      $('#send_message').attr({'disabled' : 'true', 'value' : 'Sending...' }); 

      /* Post Ajax function of jQuery to get all the data from the submission of the form as soon as the form sends the values to email.php*/ 
      $.post("email.php", $("#contact_form").serialize(),function(result){ 
       //Check the result set from email.php file. 
       if(result == 'sent'){ 
        //If the email is sent successfully, remove the submit button 
        $('#submit').remove(); 
        //Display the success message 
        $('#mail_success').fadeIn(500); 
       }else{ 
        //Display the error message 
        $('#mail_fail').fadeIn(500); 
        // Enable the submit button again 
        $('#send_message').removeAttr('disabled').attr('value', 'Send'); 
       } 
      }); 
     } 
    });  
}); 
+0

你正在使用的是什麼環境 – Anubhav

+0

@anubhav簡單的PHP你可以在http://www.khaledarajabplusfahadalmarzouq.com上查看實時版本 – Omar

+0

假設你的JS是正確的,你的PHP腳本返回'failed'。我想,當你輸入你自己的電子郵件時,你的收件箱中沒有任何東西?也許你應該嘗試手動執行它並檢查輸出或日誌以查看爲什麼'mail'語句失敗。 – CompuChip

回答

1

您的email.php在REQUEST全球範圍內具有特定的電子郵件地址,因此它在您的電子郵件地址中查找已發佈的字段。如果您要發送到特定的電子郵件地址只列出它在一個字符串像這樣:

<?php 
$subject = $_POST['subject'] . ' Ajax HTML Contact Form : Demo'; 
$to = '[email protected]'; //Recipient's E-mail 

$headers = 'MIME-Version: 1.0' . "rn"; 
$headers .= "From: " . $_POST['email'] . "rn"; // Sender's E-mail 
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn"; 

$message = 'Name: ' . $_POST['name'] . "<br>"; 
$message .= $_POST['message']; 

if (@mail($to, $subject, $message, $headers)) 
{ 
    // Transfer the value 'sent' to ajax function for showing success message. 
    echo 'sent'; 
} 
else 
{ 
    // Transfer the value 'failed' to ajax function for showing error message. 
    echo 'failed'; 
} 

>

而且你開始使用它,所以你沒在你的$消息變量還沒有明確規定?當你第一次使用它時,不需要'='。

您可能要考慮在發佈的數據中添加一些過濾器。

+0

沒有工作,它仍然顯示「對不起,這次發送錯誤時發生了錯誤。」當我點擊發送 – Omar

+0

你在你的本地機器上工作嗎?如果是這樣,你的電腦可能沒有設置發送電子郵件,你需要設置一些類似hMailServer,這裏是一個教程:http://www.codesynthesis.co.uk/tutorials/local-mail-server-with-hmailserver(或者在你的服務器上測試它) –

+0

Thanx很多工作 – Omar

相關問題