当我点击提交按钮时,我的电子邮件不发送原因?

时间:2014-11-15 14:19:34

标签: javascript php jquery

当我点击提交按钮时,它会显示来自javascript文件的消息,但它不会转到我生成的php文件..

这是我的HTML代码

<form method="post" id="contactForm" action="email_send.php">
        <div class="clearfix">
            <div class="grid_6 alpha fll">
                <input type="text" name="senderName" id="senderName" placeholder="Name *" class="requiredField" />
            </div>
            <div class="grid_6 omega flr">
                <input type="text" name="senderEmail" id="senderEmail" placeholder="Email Address *" class="requiredField email" />
            </div>
        </div>
        <div>
            <textarea name="message" id="message" placeholder="Message *" class="requiredField"></textarea>
        </div>
        <input type="submit" id="sendMessage" name="sendMessage" value="Send Email" />
    <span>  </span>
</form><!-- end form -->

我的js文件

if ($("#contactForm")[0]) {
    $('#contactForm').submit(function () {
        $('#contactForm .error').remove();
        $('#contactForm .requiredField').removeClass('fielderror');
        $('#contactForm .requiredField').addClass('fieldtrue');
        $('#contactForm span strong').remove();
        var hasError = false;
        $('#contactForm .requiredField').each(function () {
            if (jQuery.trim($(this).val()) === '') {
                var labelText = $(this).prev('label').text();
                $(this).addClass('fielderror');
                $('#contactForm span').html('<strong>*Please fill out all fields.</strong>');
                hasError = true;
            } else if ($(this).hasClass('email')) {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if (!emailReg.test(jQuery.trim($(this).val()))) {
                    var labelText = $(this).prev('label').text();
                    $(this).addClass('fielderror');
                    $('#contactForm span').html('<strong>Is incorrect your email address</strong>');
                    hasError = true;
                }
            }
        });
        if (!hasError) {
            $('#contactForm').slideDown('normal', function () {
                $("#contactForm #sendMessage").addClass('load-color');
                $("#contactForm #sendMessage").attr("disabled", "disabled").addClass("btn-success").val('Sending message. Please wait...');
            });
            var formInput = $(this).serialize();
            $.post($(this).attr('action'), formInput, function (data) {
                $('#contactForm').slideUp("normal", function () {
                    $(this).before('<div class="notification-box notification-box-success"><p><i class="fa fa-check"></i>Thanks!</strong> Your email was successfully sent. We check Our email all the time.</p></div>');
                });
            });
        }
        return false;
    });
}

我在动作中写的我的php文件是

<?php 

if(isset($_POST['senderName']) && isset($_POST['senderEmail']) && isset($_POST['message']) )
{

    $senderName = $_POST['senderName'];
    $senderEmail = $_POST['senderEmail'];
    $message = $_POST['message'];

    if(!empty($senderName) && !empty($senderEmail) && !empty($message))
    {
        if(strlen($senderName)>25 || strlen($senderEmail)>25 || strlen($message)>50 )
        {
            echo 'Maximum length reached for each field';
        } 
        else
        {
            $to = 'info@courtpiece.com';
            $subject = 'Court Piece Rung';
            $body = "Name:".$senderName."\n"."Message: ".$message;
            $header = 'From'.$senderEmail;

            if(@mail($to,$subject,$body,$header))
            {
                echo 'Thanks for Contacting Us.We\'ll in touch soon. ';
            }
            else
            {
                echo 'Sorry an error occured ';
            }
        }
    }
    else
    {
        echo 'All fields are required. ';
    }       
}

&GT;

2 个答案:

答案 0 :(得分:1)

有时这会造成问题.. 您正在使用:if(isset($_POST['senderName']) && isset($_POST['senderEmail']) && isset($_POST['message']) ){

而不是:

if(isset($_POST['sendMessage'])){
if(!empty($_POST['senderName']) && !empty($_POST['senderEmail'])){

CODE HERE
}
} 

如果没有,那么可能你错过了 - 你的电子邮件发送服务。 尝试对电子邮件发送进行测试。 mail()

答案 1 :(得分:0)

您需要压缩表单的默认行为,因此它不会向您的服务器发送请求。您可以通过调用事件参数的prevendDefault函数来执行此操作.submit(function(e)....

if ($("#contactForm")[0]) {
    $('#contactForm').submit(function (e) {
        e.preventDefault(); //to suppress the behaviour of your Form -> you will send data manually with $.post
        $('#contactForm .error').remove();
        $('#contactForm .requiredField').removeClass('fielderror');
        $('#contactForm .requiredField').addClass('fieldtrue');
        $('#contactForm span strong').remove();
        var hasError = false;
        $('#contactForm .requiredField').each(function () {
            if (jQuery.trim($(this).val()) === '') {
                var labelText = $(this).prev('label').text();
                $(this).addClass('fielderror');
                $('#contactForm span').html('<strong>*Please fill out all fields.</strong>');
                hasError = true;
            } else if ($(this).hasClass('email')) {
                var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
                if (!emailReg.test(jQuery.trim($(this).val()))) {
                    var labelText = $(this).prev('label').text();
                    $(this).addClass('fielderror');
                    $('#contactForm span').html('<strong>Is incorrect your email address</strong>');
                    hasError = true;
                }
            }
        });
        if (!hasError) {
            $('#contactForm').slideDown('normal', function () {
                $("#contactForm #sendMessage").addClass('load-color');
                $("#contactForm #sendMessage").attr("disabled", "disabled").addClass("btn-success").val('Sending message. Please wait...');
            });
            var formInput = $(this).serialize();
            $.post("email_send.php", formInput, function (data) {
                $('#contactForm').slideUp("normal", function () {
                    $(this).before('<div class="notification-box notification-box-success"><p><i class="fa fa-check"></i>Thanks!</strong> Your email was successfully sent. We check Our email all the time.</p></div>');
                });
            });
        }
        return false;
    });
}

我必须说,您建立此验证的方式还有改进的余地。 尝试使用我添加的内容再次运行表单。

报告回来!