Ajax联系表单未发送电子邮件

时间:2013-11-06 13:28:48

标签: php jquery ajax email

我正在使用ajax联系表格:

jQuery(document).ready(function () {
    // Comment or uncomment the result you want.
    // Currently, shake on error is enabled.
    // When a field is left blank, jQuery will shake the form

    /* Begin config */

    //  var shake = "Yes";
    var shake = "No";

    /* End config */

    $('#message').hide();

    // Add validation parts
    $('#contact input[type=text], #contact input[type=number], #contact input[type=email], #contact input[type=url], #contact input[type=tel], #contact select, #contact textarea').each(function () {
        $(this).after('<mark class="validate"></mark>');
    });

    // Validate as you type
    $('#email').focusout(function () {
        if (!$(this).val() || !isEmail($(this).val())) $(this).addClass('error').parent().find('mark').removeClass('valid').addClass('error');
        else $(this).removeClass('error').parent().find('mark').removeClass('error').addClass('valid');
    });

    $('#submits').click(function () {
        $("#message").slideUp(200, function () {
            $('#message').hide();

            // Kick in Validation
            $('#email').triggerHandler("focusout");

            if ($('#contact mark.error').size() > 0) {
                if (shake == "Yes") {
                    $('#contact').effect('shake', {
                        times: 2
                    }, 75, function () {
                        $('#contact input.error:first, #contact textarea.error:first').focus();
                    });
                } else $('#contact input.error:first, #contact textarea.error:first').focus();

                return false;
            }

        });
    });

    $('#contactform').submit(function () {

        if ($('#contact mark.error').size() > 0) {
            if (shake == "Yes") {
                $('#contact').effect('shake', {
                    times: 2
                }, 75);
            }
            return false;
        }

        var action = $(this).attr('action');

        $('#submits')
            .after('<img src="/system/cms/themes/default/views/partials/assets/ajax-loader.gif" class="loader" />')
            .attr('disabled', 'disabled');

        $.post(action, {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            website: $('#website').val(),
            kvk: $('#kvk').val(),
            sending: $('#sending').val(),
            webwinkel: $('#webwinkel').val(),
            pakketten: $('#pakketten').val(),
        },

        function (data) {
            $('#message').html(data);
            $('#message').slideDown();
            $('#contactform img.loader').fadeOut('slow', function () {
                $(this).remove()
            });
            $('#contactform #submits').attr('disabled', '');
            if (data.match('success') != null) $('#contactform').slideUp('slow');

        });

        return false;

    });

    function isEmail(emailAddress) {

        var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);

        return pattern.test(emailAddress);
    }

    function isNumeric(input) {
        return (input - 0) == input && input.length > 0;
    }
});

在某种程度上,如果我排除此脚本,电子邮件脚本确实有效,它就不会发送电子邮件吗?

我已经尝试了所有东西,但我无法让它工作,我认为它很奇怪,因为我在更多的地方使用这个脚本并在那里工作:(...

任何想法都是?

*更新邮件脚本*

<?php if (!isset($_SESSION)) session_start();

if(!$_POST) exit();

    if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

    $name       = $_POST['name'];
    $email      = $_POST['email'];
    $phone      = $_POST['phone'];
    $website    = $_POST['website'];
    $kvk        = $_POST['kvk'];
    $sending    = $_POST['sending'];
    $webwinkel  = $_POST['webwinkel'];
    $pakketten  = $_POST['pakketten'];
    $error      = '';

    if (isset($_POST['verify'])) :
        $posted_verify   = $_POST['verify'];
        $posted_verify   = md5($posted_verify);
    else :
        $posted_verify = '';
    endif;

    // Important Variables
    $session_verify = $_SESSION['verify'];

    if (empty($session_verify)) $session_verify = $_COOKIE['verify'];



    if($error == '') {

    if(get_magic_quotes_gpc()) {
            $comments = stripslashes($comments);
        }

     // Configuration option.

     $address = "email@email.com";


     // Configuration option.
     // i.e. The standard subject will appear as, "You've been contacted by John Doe."

     // Example, $e_subject = '$name . ' has contacted you via Your Website.';

     $e_subject = 'Nieuwe Aanmelding';


     // Configuration option.
     // You can change this if you feel that you need to.
     // Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

     $e_body  = "Er is een nieuwe aanmelding binnen gekomen:\n\n" . PHP_EOL . PHP_EOL;
     $e_name  = "Name: $name \n\n";
     $e_email = "Email: $email \n\n";
     $e_phone = "Tel: $phone \n\n";
     $e_kvk   = "KVK: $kvk \n\n";
     $e_website   = "Website: $website \n\n";
     $e_sending = "Verzending nu: $sending \n\n";
     $e_winkel= "Webwinkel: $webwinkel \n\n";
     $e_pakketten= "Paketten per maand: $pakketten \n \n";

     $msg = wordwrap($e_body . $e_name . $e_email . $e_website  . $e_phone . $e_kvk . $e_sending . $e_winkel . $e_pakketten,70);

     $headers = "From: $email" . PHP_EOL;
     $headers .= "Reply-To: $email" . PHP_EOL;
     $headers .= "MIME-Version: 1.0" . PHP_EOL;
     $headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
     $headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

     if(mail($address, $e_subject, $msg, $headers)) {


     // Email has sent successfully, echo a success page.

     echo "<div class='span12'>";
     echo "<div class='span4'></div>";
     echo "<div class='span6'>";
     echo "<div id='success_page'>";
     echo "<h1 style='color:white'>Uw aanmelding is verzonden.</h1>";
     echo "<p style='color:white'>Bedankt <strong>$name</strong>, U wordt zo spoedig mogelijk geholpen.</p>";
     echo "</div>";
     echo "</div>";

     } else {

     echo 'ERROR!';

     }

}
 ?>

解决

各位,

感谢您的快速回复。

这是刚刚下载的新jquery,现在正在运行。

$('#contactform').submit(function(){

    if ($('#contact mark.error').size()>0) {
        if(shake == "Yes") {
        $('#contact').effect('shake', { times:2 }, 75);
        }
        return false;
    }

    var action = $(this).attr('action');

    $('#submit')
        .after('<img src="assets/img/ajax-loader.gif" class="loader" />')
        .attr('disabled','disabled');

    $.post(action, $('#contactform').serialize(),
        function(data){
            $('#message').html( data );
            $('#message').slideDown();
            $('#contactform img.loader').fadeOut('slow',function(){$(this).remove()});
            $('#contactform #submit').removeAttr('disabled');
            if(data.match('success') != null) $('#contactform').slideUp('slow');

        }
    );

    return false;

});

1 个答案:

答案 0 :(得分:0)

在如此多的级别上有这么多错误,请先来看看www.phptherightway.com。

之后,尝试确定表单是否正在发送到服务器(通过POST请求方法)。如果不是,我猜测表单在提交事件中返回false,这就是它最不可能工作的原因。