在接触形状误差通过jQuery AJAX提交 [英] error on contact form submit via jquery ajax
问题描述
根据此<一个href="http://stackoverflow.com/questions/15767317/initialize-javascript-in-php-script-for-a-contact-form">question我不得不重写我的联系方式的脚本。
我们的目标是有一个发送按钮标记发送
。 clickick后,它应该显示发送
直到PHP脚本完成。当它做它应该显示发送
。
Based on this question i had to rewrite my contact form script.
The goal is to to have a send button labeled send
. After clickick it should show sending
till the php script is done. When its done it should show sent
.
这就是我的简单的形式:
Thats my simple form:
<form id="contactForm" action="mail.php" method="post">
<input type="text" id="name" name="name" placeholder="Name" required><br />
<input type="text" id="email" name="email" placeholder="Mail" required><br />
<textarea name="message" id="message" placeholder="Nachricht" required></textarea><br />
<button name="submit" type="submit" id="submit">send</button>
</form>
下面是为标签的jQuery脚本改变和AJAX提交。
Here is the jquery script for the label changing and the ajax submit.
<script>
$( init );
function init() {
$('#contactForm').submit( submitForm );
}
function submitForm() {
var contactForm = $(this);
if ( !$('#name').val() || !$('#email').val() || !$('#message').val() ) {
$('#submit').html('error');
} else {
$('#submit').html('sending');
$.ajax( {
url: contactForm.attr( 'action' ) + "?ajax=true",
type: contactForm.attr( 'method' ),
data: contactForm.serialize(),
success: submitFinished
} );
}
return false;
}
function submitFinished( response ) {
response = $.trim( response );
if ( response == "success" ) {
$('#submit').HTML = ('sent');
} else {
$('#submit').html('error');
}
}
</script>
在mail.php:
the mail.php:
<?php
define( "RECIPIENT_NAME", "John Doe" );
define( "RECIPIENT_EMAIL", "john@doe.com" );
define( "EMAIL_SUBJECT", "Subject" );
$success = false;
$name = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$email = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
if ( $name && $email && $message ) {
$recipient = RECIPIENT_NAME . " <" . RECIPIENT_EMAIL . ">";
$headers = "Von: " . $name . " <" . $email . ">";
$success = mail( $recipient, EMAIL_SUBJECT, $message, $headers );
}
if ( isset($_GET["ajax"]) ) {
echo $success ? "success" : "error";
} else {
//add html for javascript off user
}
?>
它的提交正确的,我得到的邮件,但我不改变的标签发送
。其停留在发送
。
任何想法或建议,什么是错我的code?
Its submits correct and i get the mail but i doesnt change the label to sent
.Its stuck at sending
.
Any idea or suggestions whats wrong with my code?
最好的问候 dennym
best regards dennym
推荐答案
该错误是在这条线:
$('#submit').HTML = ('sent');
改成:
$('#submit').html('sent');
这篇关于在接触形状误差通过jQuery AJAX提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!