PHP联系表单,JS验证不发送电子邮件

时间:2014-01-14 08:14:31

标签: javascript php forms email

我在这里做错了联系表格不发送和发送电子邮件我收到错误消息"抱歉,这次发送您的消息时出错。"当我填写表格并点击发送

  

的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['omar@cinqomedia.com']; //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');
                }
            });
        }
    });    
});

1 个答案:

答案 0 :(得分:1)

您的email.php在REQUEST全球范围内具有特定的电子邮件地址,因此它会查找您的电子邮件地址的已过帐字段。如果您想发送到特定的电子邮件地址,只需将其列在如下字符串中:

<?php
$subject = $_POST['subject'] . ' Ajax HTML Contact Form : Demo';
$to = 'omar@cinqomedia.com'; //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';
}

&GT;

在开始使用它之前,还没有定义你的$ message变量,因此当你第一次使用它时,你不需要'。='只是'='。

您可能需要考虑在发布的数据中添加一些过滤器。