2

基于这个问题,我不得不重写我的联系表单脚本。目标是有一个标记为 的发送按钮send。clickick 后它应该显示sending直到 php 脚本完成。当它完成它应该显示sent

那是我的简单形式:

<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>

这是用于标签更改和 ajax 提交的 jquery 脚本。

<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>

邮件.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
}
?>

它提交正确,我收到了邮件,但我没有将标签更改为。sent它卡在了sending。任何想法或建议我的代码有什么问题?

最好的问候丹尼姆

4

3 回答 3

3

错误在这一行:

$('#submit').HTML = ('sent');

将其更改为:

$('#submit').html('sent');
于 2013-04-02T16:50:22.860 回答
2
$('#submit').HTML = ('sent');

应该

$('#submit').html('sent');

就像你在其他地方一样。

于 2013-04-02T16:50:14.740 回答
2

你必须改变

$('#submit').HTML = ('sent');

到:

$('#submit').html('sent');

在你的功能中submitFinished()

于 2013-04-02T16:52:40.923 回答