1

所以我得到了这个 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";
}

我只是收到警报“这不起作用。再试一次?”

4

1 回答 1

4

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

网站邮箱: yes@test.com

website-message:测试数据

虽然您的 PHP 代码正在查找 $_POST['email'],但它应该是 $_POST['website-email'];

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

于 2013-03-17T19:58:07.940 回答