简体   繁体   English

AJAX联络表格无效

[英]AJAX Contact Form is NOT working

I have a problem with my AJAX Contact Form, every thing I tried failed. 我的AJAX联络表单有问题,我尝试的每件事都失败了。 I allways get the error from the jQuery Part if(html==0) . 我总是从jQuery部分if(html==0)得到错误。

Someone please help, can't find the problem. 有人请帮忙,找不到问题。 Thank you! 谢谢!

jQuery Part: action.js jQuery部分: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 Part: action.php 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;
    }
} ?>

Try to change your initilization data 尝试更改您的初始化数据

 $.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!');
                }
            }
        });

I would suggest you the use of json to parse the data. 我建议您使用json解析数据。

So your javascript code would look like this: 因此,您的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 */
});

And your PHP code would look like this: 您的PHP代码将如下所示:

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

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

Your headers need to end with newlines. 标头必须以换行符结尾。 Single quotes will just put out the actual characters \\r\\n , while double quotes will convert them to new lines. 单引号将只输出实际字符\\r\\n ,而双引号会将其转换为新行。 Your mail is probably failing due to bad headers, so adjust them like this: 您的邮件可能由于标题错误而失败,因此请按以下步骤调整它们:

$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";

If it's still not sending the email, you'll need to look at your mail server logs to see where the issue may lie. 如果仍然没有发送电子邮件,则需要查看您的邮件服务器日志以查看问题所在。

That's the solution: 那是解决方案:

$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";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM