为什么要提交此表单?

时间:2013-06-01 02:43:50

标签: php javascript ajax

我有一个问题,我想通过AJAX邮寄,我的问题是我的表单一直在提交。 由于某种原因,它刷新了URL中的所有表单,但我没有发送GET的任何内容?

HTML:

<form onsubmit="ajaxEmail();
        return false;" >
    <input type="text" name="name" id="name" value="Name" maxlength="1000" onfocus="this.value = this.value == 'Name' ? '' : this.value;" onblur="this.value = this.value == '' ? 'Name' : this.value;" />
    <input type="email" name="email" id="email" value="Email" maxlength="1000" onfocus="this.value = this.value == 'Email' ? '' : this.value;" onblur="this.value = this.value == '' ? 'Email' : this.value;" />
    <input type="text" id="subject"  value="Subject" maxlength="2000" onfocus="this.value = this.value == 'Subject' ? '' : this.value;" onblur="this.value = this.value == '' ? 'Subject' : this.value;" />
    <textarea name="Message" id="body" maxlength="40000" onfocus="this.value = this.value == 'Message' ? '' : this.value;" onblur="this.value = this.value == '' ? 'Message' : this.value;">Message</textarea>
    <input type="submit" value="send" class="submit-button"/>
</form>

JS:

$(function ajaxEmail() {
    $('#emailMsgResp').hide();
    $('#emailMsgBody').hide();

    var name = $('#name').val();
    var emailForm = $('#email').val();
    var subject = $('#subject').val();
    var message = $('#body').val();

    $.ajax({
        type: "POST", // Can be "GET"
        url: "../ajaxMail.php",
        data: {
        name: name,
        emailFrom: emailFrom,
        subject: subject,
        message: message
    },
    dataType: "json",
    success: function (data) {

        if (data['response'] == 1) {

            $('#emailMsgResp').show();
            $('#emailMsgResp').append('<div class="skill3" id="emailMsgBody" style="width:325px; height: 38px; margin: 0px auto;"><p>Thank you for contacting me' + data['response'] + '</p></div>');
            return false;

        } else if (data['response'] == 0) {

            $('#emailMsgResp').show();
            $('#emailMsgResp').append('<div class="skill3" id="emailMsgBody" style="width:325px; height: 38px; margin: 0px auto;"><p>Error: Please use your own client and write to contact@ivan-ristic.com</p></div>');
            return false;
        }
    }
    });
    return false;
});

ajaxMail.php

$email_it_to = 'mymail@mymail.com';

$name = $_POST['name'];
$email_from = $_POST['emailFrom'];
$subject = $_POST['subject'];
$body = $_POST['message'];

if(!isset($name) || !isset($email_from) || !isset($subject) || !isset($body)) {
    $response = 0;

} else {

    $email_from = $email;
    $email_subject = "Contact Form: ".stripslashes($subject);
    $email_message = "Below is a message submitted by '".stripslashes($name);
    $email_message .="' on ".date("d/m/Y")." at ".date("H:i")."\n\n";
    $email_message .= stripslashes($body);

    $headers = 'From: '.$email_from."\r\n" .

    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();

    if (mail($email_it_to, $email_subject, $email_message, $headers)) {

        $response = 1;
    } else {

        $response = 0;
    }
}

echo json_encode($response);

1 个答案:

答案 0 :(得分:7)

您将函数指定为文档就绪处理程序,因此一旦页面准备就会调用它。你有:

$(function ajaxEmail() {
   /* your code */
});

......相当于:

$(document).ready(function ajaxEmail() {
    /* your code */
});

应该拥有的内容:

function ajaxEmail() {
   /* your code */
}

这将使您的函数成为可以从内联事件属性调用的全局函数。

或者,既然您正在使用jQuery,请从表单中删除内联onsubmit=...处理程序,并使用jQuery分配它:

$(function() {
    $("form").submit(function(e) {
        e.preventDefault();

        /* your other function contents here */
    });
});

当然最好为表单提供一个id属性并使用$("#yourFormId").submit(...)而不是使用$("form").submit(...)(它为页面上的所有表单指定相同的处理程序)。