使用jQuery AJAX的表单PHP不会在电子邮件中发送字段输入

时间:2015-04-27 04:14:43

标签: javascript php jquery ajax forms

我有这个联系表单,我正在尝试使用jQuery / AJAX和PHP将字段输入值通过电子邮件发送给我。嗯,电子邮件发送给我很好,但没有任何字段输入。电子邮件只是PHP mail()函数的$ to和$ subject。请参阅下面的代码。任何想法,批评或建议都是受欢迎的。谢谢你的帮助!

$(document).ready(function() {    

$(function() {

// Get the form.
var form = $('#ajax-contact');

// Get the messages div.
//var formMessages = $('#form-messages');

// Set up an event listener for the contact form.
$("button").click(function(e) {

    // Stop the browser from submitting the form.
    e.preventDefault(e);

    // Serialize the form data.
    var formData = $(form).serialize();

    // Submit the form using AJAX.
    $.ajax({
        type: 'POST',
        url: $(form).attr('action'),
        data: formData,
        success: function(result){
            $('#contact-name').val('');
            $('#contact-email').val('');
            $('#contact-website').val('');
            $('#contact-message').val('');
        },
        error: function(xhr,status,error){

        }
    });


});

});

});

<?php
require_once 'swiftmailer/lib/swift_required.php';

//Swift Mailer script

$filter = FILTER_SANITIZE_STRING;

//Grab POST Data
$name = filter_var($_POST['contact-name'], $filter);
$email = filter_var($_POST['contact-email'], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST['contact-website'], FILTER_SANITIZE_URL);
$message = filter_var($_POST['contact-message'], $filter);


//Create email body
$body = "Name: " . $name . "<br />" . "Email: " . $email . "<br />" .   "Website: " . $website . "<br />" . "Message: " . $message;

//Create Transport
$transport = Swift_SmtpTransport::newInstance("host", "port", "security")
->setUsername("username")
->setPassword("password");

//Create Mailer
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('New Form')
->setFrom(array("email" => "Lead from Contact form"))
->setTo(array('email', 'email' => 'Recipients'))
->setSubject('You have a new message!')
->setBody($body, 'text/html');

//Send Message
$result = $mailer->send($message);

if ($result) {

//设置200(好的)响应代码。             // http_response_code(200);             echo“谢谢!您的邮件已发送。”;     } else {     //设置500(内部服务器错误)响应代码。             // http_response_code(500);             回声“哎呀!出了点问题,我们无法发送你的消息。”;     }

?>

3 个答案:

答案 0 :(得分:6)

使用val()函数从输入中获取数据

$(document).ready(function(e) {

$("button").click(function(e) {
e.preventDefault();
    var ajax = {
        isSubmitting: false,
        send: function() {
            if(ajax.isSubmitting == false) {

                ajax.isSubmitting = true;
                var userName = $('input[name="contact-name"]').val(); 
                var userEmail = $('input[name="contact-email"]').val(); 
                var userWebsite = $('input[name="contact-website"]').val(); 
                var userMessage = $('input[name="contact-message"]').val(); 

                if(userName === "" || userEmail === "" || userWebsite === "" || userMessage === "") {
                    alert("Please fill out all required fields.");
                } 
                else {
                    $.post("mailer3.php", {
                        name: userName,
                        email: userEmail,
                        website: userWebsite,
                        message: userMessage

                    }, function(data) {

                        ajax.isSubmitting = false;

                    });
                }
            }
            else alert("Send only 1 email at a time.");
        }
    }

});

尝试使用$ .ajax:

$("button").click(function(){
     isSubmitting: false;
     var userName = $('input[name="contact-name"]').val(); 
     var userEmail = $('input[name="contact-email"]').val(); 
     var userWebsite = $('input[name="contact-website"]').val(); 
     var userMessage = $('input[name="contact-message"]').val(); 

    if(isSubmitting == false) {

    $.ajax({url: "mailer3.php",
            type:'POST',
            data:{
            name: userName,
            email: userEmail,
            website: userWebsite,
            message: userMessage
             },
            success: function(result){
               isSubmitting == true;
            },error(xhr,status,error){
               isSubmitting == false;
            }
    });

    }
});

答案 1 :(得分:1)

因为你没有使用jQquery val();检索输入值的方法whioch是你无法获得价值的原因。使用链式方法val()和调用的元素如下所示

`$(document).ready(function(e){ //e.preventDefault(); $(“按钮”)。点击(功能(e){

var ajax = {
    isSubmitting: false,
    send: function() {
        if(ajax.isSubmitting == false) {

            ajax.isSubmitting = true;
            var userName = $("input[name=contact-name]").val(); 
            var userEmail = $("input[name=contact-email]").val(); 
            var userWebsite = $("input[name=contact-website]").val(); 
            var userMessage = $("input[name=contact-message]").val(); 

            if(userName === "" || userEmail === "" || userWebsite === "" || userMessage === "") {
                alert("Please fill out all required fields.");
            } 
            else {
                $.post("mailer3.php", {
                    name: userName,
                    email: userEmail,
                    website: userWebsite,
                    message: userMessage

                }, function(data) {

                    ajax.isSubmitting = false;

                });
            }
        }
        else alert("Send only 1 email at a time.");
    }
} `

答案 2 :(得分:0)

我最终确定了这一点。我修改后的代码位于顶部,是我用来获取带有数据的电子邮件。使用SwiftMailer。只需完成一些事情。感谢大家对此的投入!

安德鲁