【问题标题】:error on contact form submit via jquery ajax通过 jquery ajax 提交联系表单时出错
【发布时间】:2013-03-24 01:47:08
【问题描述】:

基于此question,我不得不重写我的联系表单脚本。 目标是有一个标记为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>

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

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

最好的问候 丹尼姆

【问题讨论】:

  • 当您在 Firebug 或 Dev Tools 中查看请求时,输出是什么?
  • 试着把 HTML 变成 html
  • 同时尝试并提醒响应以确保它显示成功

标签: php jquery html ajax forms


【解决方案1】:

错误在这一行:

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

将其更改为:

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

【讨论】:

  • -.- 这一定是因为一些自动完成,不知道,但这有帮助
【解决方案2】:
$('#submit').HTML = ('sent');

应该是

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

就像你在其他地方一样。

【讨论】:

    【解决方案3】:

    你必须改变

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

    到:

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

    在你的函数submitFinished();

    【讨论】:

      猜你喜欢
      • 2011-04-21
      • 1970-01-01
      • 2018-12-24
      • 2012-03-22
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      相关资源
      最近更新 更多