简体   繁体   English

AJAX联系表格的PHP代码

[英]PHP code for AJAX contact form

I was trying to to make a contact form working with jQuery AJAX and PHP. 我试图制作一个使用jQuery AJAX和PHP的联系表单。 My HTML is as follows 我的HTML如下

<form action="#" id="contactForm" method="post" name="contactForm">
<fieldset>
    <div class="col-sm-12">
        <input id="contactName" name="contactName" placeholder="Your Name*" type="text" value="" required>
    </div>

    <!-- Name Field [ END ] -->

    <div class="col-sm-12">
        <input id="contactEmail" name="contactEmail" placeholder="Your Email*" type="email" value="" required>
    </div>

    <!-- Email Field [ END ] -->

    <div class="col-xs-12">
        <textarea cols="5" id="contactMessage" name="contactMessage" placeholder="Your Message....*" required></textarea>
    </div>

    <!-- Message Field [ END ] -->

    <div class="col-xs-12">
        <button class="submit">SEND</button>
    </div>

    <!-- Submit Button [ END ] -->

    <div class="error col-xs-12">
        <h3>Sorry! Your message was not sent.</h3>
    </div>

    <!-- Error Message [ END ] -->

    <div class="success col-xs-12">
        <h3>Success! Your message was sent.</h3>
    </div>

    <!-- Seccess Message [ END ] -->

</fieldset>

while jQuery code is as follows 而jQuery代码如下

$('form#contactForm').submit(function () {

    var url = $('#contactForm').attr('action'),
        type = $('#contactForm').attr('method'),

        contactName = $('#contactForm #name').val(),
        contactEmail = $('#contactForm #email').val(),
        contactMessage = $('#contactForm #message').val(),
        data = {
            name: contactName,
            email: contactEmail,
            message: contactMessage
        };

    $.ajax({
        type: type,
        url: url,
        data: data,
        success: function () {
            $('.success').fadeIn();
            $('.error').fadeOut();
        },
        error: function () {
            $('.error').fadeIn();
            $('.success').fadeOut();
        }
    });
    return false;
});

what could be possible PHP code to make this form working. 使该表单正常工作的PHP代码可能是什么。 I am using following code 我正在使用以下代码

 <?php

    if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {

        $to = 'mail@example.com'; //my email address here
        $subject = 'Message From :' . $_POST['name'];
        $message = $_POST['message'];
        $headers = 'From :' . $_POST['email'].'\r\n';

        mail($to,$subject,$message,$headers);

    }
?>

but it is not working. 但它不起作用。 Success message shows but mail do not received. 显示成功消息,但未收到邮件。 I am not familiar with PHP very well. 我对PHP不太熟悉。

Please Help.... 请帮忙....

Your sucess message shows becasue the php-page was called successfully, this does not indicate that the mail was sent successfully. 您的成功消息显示,因为成功调用了php页,但这并不表示邮件已成功发送。 You should check this by returning the mail-functions response: 您应该通过返回邮件功能响应进行检查:

$response = mail($to,$subject,$message,$headers);
echo $response;

This will return true or false. 这将返回true或false。 Also you will have to check that the mail is being recieved correctly, it could end up in spam, or be rejected by your mailserver for various reasons. 另外,您还必须检查是否正确接收了邮件,它可能最终成为垃圾邮件,或者由于各种原因而被您的邮件服务器拒绝。 There are some additions you can do to the mail-header if this is the case. 在这种情况下,您可以对邮件标题进行一些附加操作。 Here are some pointers on how to get the mail through 以下是有关如何通过邮件的一些提示

Try adding 尝试添加

method: "POST",

In your $.ajax call, by default $.ajax uses GET requests $.ajax调用中,默认情况下$ .ajax使用GET请求

This will Work for you for you 这将为您服务

$('form#contactForm').submit(function () {

    var url = $('#contactForm').attr('action');
        var type = $('#contactForm').attr('method');

        var contactName = $('#contactName').val(); 
        var contactEmail = $('#contactEmail').val(); 
         var contactMessage = $('#contactMessage').val();
        data = {
            name: contactName,
            email: contactEmail,
            message: contactMessage
        };console.log(data);

    $.ajax({
        type: type,
        url: url,
        data: data,
        success: function () {
            $('.success').fadeIn();
            $('.error').fadeOut();
        },
        error: function () {
            $('.error').fadeIn();
            $('.success').fadeOut();
        }
    });
    return false;
});

Your PHP Code becomes 您的PHP代码变为

<?php
if ( isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {

    $to = $_POST['email']; //my email address here
    $subject = 'Message From :' . $_POST['name'];
    $message = $_POST['message'];
    $headers = 'From :' . $_POST['email'].'\r\n';

    mail($to,$subject,$message,$headers);

}
        echo 'sucess';
?> 

And change here too 并在这里也改变

<form action="===GIVE YOUR FILE NAME CONTAINING PHP mail() CONTAINS====" id="contactForm" method="post" name="contactForm">

And You should confirm that your server has enabled with mail() function 并且您应该确认您的服务器已启用mail()函数

Try this and let me know.. 试试这个,让我知道..

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

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