我有一個問題,我想通過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 [email protected]</p></div>');
return false;
}
}
});
return false;
});
ajaxMail.php
$email_it_to = '[email protected]';
$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);
'$(function ajaxEmail(){/ * code * /});'表示在頁面加載時執行該函數。如果你正在使用jQuery,利用它的事件綁定,通過給予'
什麼時候是第一次提交?在輸入鍵上? –
Both ----------- –