AJAX联系表单无法发布数据

时间:2013-03-17 19:45:28

标签: php javascript jquery ajax forms

所以我有这个AJAX联系表格,我以前用过的代码。我无法解决为什么它不起作用。

HTML
<div id="website-contact-form">
<form id="website_contact" name="website_contact">
        <input id="email-address-input" name="website-email" type="text" placeholder="Your email here" class="order-form-input" /><br />
        <textarea name="website-message" placeholder="Please give a brief description of what you have in mind, plus contact details." class="order-form-textarea"></textarea>
        <a href="#" id="submit-website-project" name="submit-website-project" class="send-button"></a>
</form>
</div>

JS

<script type="text/javascript">
$(document).ready(function(){
    $('#submit-website-project').click(function (e) {

        e.preventDefault();
        if ($('#email-address-input').val() != ""){

                postForm("ajax/contact-website.php", "website_contact",
                function (data) {

                    if (data == "success") {
                        $('#website-contact-form').
                        html("<br />Thankyou for your enquiry. I'll "+
                             "get in touch shortly.");
                    } else {

                    alert("That didn't work. Try again?");
                    }
                });
        }
    });

}); //END DOCUMENT READY

function postForm(url, form_id, success_func) {
    $.ajax({
        type: "POST",
        url: url,
        data: $("#" + form_id).serialize(),
        success: function (data) {
            success_func(data);
        }
    });
}
</script>

最后我的PHP

<?php

if (isset($_POST['email'])) {
    $_POST['email'] = trim(@$_POST['email']);

    $ToEmail = 'barneywimbush:gmail.com';
    $EmailSubject = 'Barneywimbush.com';
    $mailheader = "From: ".$_POST["email"]."\r\n";
    $mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
    $mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";

    $MESSAGE_BODY = "Email: ".$_POST["email"]."";
    $MESSAGE_BODY .= "Comment: ".nl2br($_POST["project_description"])."";

    $res = mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader); // or die ("Failure")

    if ($res) {
        echo "success";
    }
    else {
        echo "failed";
    }
}
else {
    echo "failed";
}

我只是收到提醒“那不起作用。再试一次?”

1 个答案:

答案 0 :(得分:4)

您的$ _POSTs的PHP变量不正确,在检查“网络”面板时,您的表单正在发送,

网站 - 电子邮件: yes@test.com

website-message:测试数据

虽然您的PHP代码看起来是$ _POST ['email'],但它应该是$ _POST ['website-email'];

更改输入元素的name属性以更改请求的参数名称。