开发者

PHP Mail & Ajax: Message does not contain any form data

开发者 https://www.devze.com 2023-01-09 15:39 出处:网络
I\'m using jQuery\'s AJAX function to send a message from a contact form - $(\'form button\').click(function () {

I'm using jQuery's AJAX function to send a message from a contact form -

$('form button').click(function () {

    $("input").removeClass("error");
    $("textarea").removeClass("error");

    var name = $("#name").val();
    if (name == "" || name == "Name" || name == "Namn") {
        $("#name").addClass("error");
        $("#name").focus();
        return false;
    }

    var email = $("#email").val();
    if (email == "" || email == "Email" || email == "Epost") {
        $("#email").addClass('error');
        $("#email").focus();
        return false;
    }

    var message = $("#message").val();
    if (message == "") {
        $("#message").addClass('error');
        $("#message").focus();
        return false;
    }

    // Non-verifying fields
    var phone = $("input#phone").val();

    // Gather data
    var post = 'name=' + name + '&email=' + email + '&phone=' + phone + '&message=' + message;

    // Disable form
    var limit = document.forms[0].elements.length;
    for (i = 0; i < limit; i++) {
        document.forms[0].elements[i].disabled = true;
    }

    // Send data
    $.ajax({
        type: "POST",
        url: "form_handler.php",
        data: post,
        success: function () {
            $('div.contact form').animate({
                opacity: 0.25
            }, function () {
                $('div.contact div.confirm').fadeIn(200);
            });
        },
        error: function () {
            $('div.contact form').animate({
                opacity: 0.25
            }, function () {
                $('div.contact div.deny').fadeIn(200);
            });
        }
    });
    return false;
});

I know this is not the safest method considering I reveal the Mail file in my JS code but nevertheless I want this to work before I decide to try anything else. In my contact form I have the above fields (name, email, phone and message) and in "form_handler.php" the settings look like this -

<?php
header('Content-type: text/html; charset=UTF-8');

$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$mess开发者_如何学Goage = $_POST['message'];

$to = "staffan.estberg@gmail.com";
$subject = "Meddelande från x.se";
$body = "------------------------------------------------------------------------------------------------------------------------\n\n";
$body .= "Meddelande från $name:\n\n";
$body .= "$message\n\n";
$body .= "Avsändarens epost: $email\n";
$body .= "Avsändarens telefonnummer: $phone\n\n";
$body .= "------------------------------------------------------------------------------------------------------------------------";
$headers = "From: $email";

mail($to,$subject,$body,$headers);
?>

When I combine the scripts I manage to generate a message though it doesn't contain any form data. Have I missed out on something?


I think you'll actually want to pass a JSON array as the data parameter instead of the GET-style string.

Something like:

post = { 'name' : name, ... }
0

精彩评论

暂无评论...
验证码 换一张
取 消