2014-01-14 65 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