[英]error on contact form submit via jquery ajax
Based on this question i had to rewrite my contact form script. 基于这个问题,我不得不重写我的联系表格脚本。 The goal is to to have a send button labeled
send
. 目的是要有一个标记为
send
的发送按钮。 After clickick it should show sending
till the php script is done. 点击后它应该显示
sending
直到PHP脚本完成。 When its done it should show sent
. 完成后应显示已
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>
Here is the jquery script for the label changing and the ajax submit. 这是标签更改的jquery脚本和ajax提交。
<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>
the mail.php: 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
}
?>
Its submits correct and i get the mail but i doesnt change the label to sent
.Its stuck at sending
. 它的提交正确的,我得到的邮件,但我并没有改变的标签
sent
停留在。它的sending
。 Any idea or suggestions whats wrong with my code? 任何想法或建议我的代码有什么问题吗?
best regards dennym 最好的问候
The error is on this line: 错误在这一行:
$('#submit').HTML = ('sent');
Change that to: 改为:
$('#submit').html('sent');
$('#submit').HTML = ('sent');
should be 应该
$('#submit').html('sent');
like you have everywhere else. 就像你在其他地方一样。
You have to change 你必须改变
$('#submit').HTML = ('sent');
to: 至:
$('#submit').html('sent');
in your function submitFinished()
; 在您的函数
submitFinished()
;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.