AJAX联系表格不起作用

时间:2016-08-03 14:45:27

标签: php jquery ajax email

我的AJAX联系表单有问题,我尝试的每件事都失败了。我总是从jQuery Part if(html==0)中得到错误。

有人请帮忙,找不到问题。谢谢!

jQuery Part:action.js

$(function() {

    /* EMAIL VALIDATOR */
    function isEmail(email) {
        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][\d]\.|1[\d]{2}\.|[\d]{1,2}\.))((25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\.){2}(25[0-5]|2[0-4][\d]|1[\d]{2}|[\d]{1,2})\]?$)/i);
        return pattern.test(email);
    }

    /* CONTACT */
    $('#contact-submit').click(function() {
        var firstname = $('#contact-firstname').val();
        if(!firstname.trim()) {
            $('#contact-firstname').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie Ihren Vornamen ein!</span>');
            return false;
        }
        var lastname = $('#contact-lastname').val();
        if(!lastname.trim()) {
            $('#contact-lastname').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie Ihren Nachnamen ein!</span>');
            return false;
        }
        var email = $('#contact-email').val();
        if(!email.trim()) {
            $('#contact-email').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie Ihre E-Mail ein!</span>');
            return false;
        } else {
            if(!isEmail(email)) {
                $('#contact-email').focus();
                $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie eine g&uuml;ltige E-Mailadresse ein!</span>');
                return false;  
            }
        }
        var phone1 = $('#contact-phone1').val();
        if(!phone1.trim()) {
            $('#contact-phone1').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie Ihre Landesvorwahl ein!</span>');
            return false;
        }
        var phone2 = $('#contact-phone2').val();
        if(!phone2.trim()) {
        $('#contact-phone2').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie Ihre Telefonnummer ein!</span>');
            return false;
        }
        var phone = phone1+' (0) '+phone2;
        var subject = $('#contact-subject').val();
        if(!subject.trim()) {
            $('#contact-subject').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie einen Betreff ein!</span>');
            return false;
        }
        var message = $('#contact-message').val();
        if(!message.trim()) {
            $('#contact-message').focus();
            $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Bitte geben Sie eine Nachricht ein!</span>');
            return false;
        }

        $.ajax({
            type:   'POST',
            url:    'action.php',
            data:   'contact=1&firstname='+firstname+'&lastname='+lastname+'&email='+email+'&phone='+phone+'&subject='+subject+'&message='+message,
            dataType: 'html',
            success: function(html) {
                if(html==0) {
                    $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Nachricht konnte nicht gesendet werden!</span>');
                } else if(html==1) {
                    $('#contact-result').html('Danke f&uuml;r Ihre Nachricht!');
                } else {
                    alert('Fehler beim Senden!');
                }
            }
        });
        return false;
    });
});

PHP部分:action.php

<? if($_POST['contact']) {
    $contactEmail = 'contact@pixel-factory.at';

    $firstname  = $_POST['firstname'];
    $lastname   = $_POST['lastname'];
    $email      = $_POST['email'];
    $phone      = $_POST['phone'];
    $subject    = $_POST['subject'];
    $message    = $_POST['message'];

    $mailSubject = 'KONTAKTANFRAGE '.$subject;

    $mailMessage =  $message.'\r\n'.'\r\n';
    $mailMessage .= 'Vorname: '.$firstname.'\r\n';
    $mailMessage .= 'Nachname: '.$lastname.'\r\n';
    $mailMessage .= 'E-Mail: '.$email.'\r\n';
    $mailMessage .= 'Telefonnummer: '.$phone;

    $mailHeader =   'MIME-Version: 1.0'.'\r\n';
    $mailHeader .=  'Content-type:text/html;charset=UTF-8'.'\r\n';
    $mailHeader .=  'From: Kontkatformular'.'\r\n';
    $mailHeader .=  'Reply-To: '.$email.'\r\n';

    $sendMail = mail($contactEmail, $mailSubject, $mailMessage, $mailHeader);

    if($sendMail) {
        echo 1;
    } else {
        echo 0;
    }
} ?>

4 个答案:

答案 0 :(得分:0)

尝试更改您的启动数据

 $.ajax({
            type:   'POST',
            url:    'action.php',
            data:   {contact: 1, firstname:firstname,lastname:lastname, email:email, phone:phone, subject:subject, message:message},
            dataType: 'html',
            success: function(html) {
                if(html==0) {
                    $('#contact-result').html('<span class="error"><i class="fa fa-exclamation-triangle fa-fw"></i>&nbsp;Nachricht konnte nicht gesendet werden!</span>');
                } else if(html==1) {
                    $('#contact-result').html('Danke f&uuml;r Ihre Nachricht!');
                } else {
                    alert('Fehler beim Senden!');
                }
            }
        });

答案 1 :(得分:0)

我建议你使用json来解析数据。

所以你的javascript代码看起来像这样:

$.post('action.php', 
{
   'contact'   : 1,
   'firstname' : firstname,
   'lastname'  : lastname
}).done(function(response)
{
   var json = JSON.parse(response);

   if(json.status == false)
      /* do something */

   else if(json.status == true)
      /* do something */

   else /* This might never happen.. */
      /* do something else */
});

您的PHP代码如下所示:

$status = $sendMail == TRUE ? TRUE : FALSE;

echo json_encode(array
(
   'status' => $status
));

答案 2 :(得分:0)

您的标题需要以换行符结尾。单引号只会输出实际字符\r\n,而双引号会将它们转换为新行。由于标题错误,您的邮件可能会失败,因此请按以下方式调整它们:

$mailHeader =   'MIME-Version: 1.0'."\r\n";
$mailHeader .=  'Content-type:text/html;charset=UTF-8'."\r\n";
$mailHeader .=  'From: Kontkatformular'."\r\n";
$mailHeader .=  'Reply-To: '.$email."\r\n";

如果仍然没有发送电子邮件,您需要查看邮件服务器日志以查看问题所在。

答案 3 :(得分:0)

这就是解决方案:

$mailHeader =   'MIME-Version: 1.0'."\r\n";
$mailHeader .=  'Content-type:text/html;charset=UTF-8'."\r\n";
$mailHeader .=  'From: Kontkatformular <no-reply@pixel-factory.at>'."\r\n";
$mailHeader .=  'Reply-To: '.$email."\r\n";