jQuery.validate 插件和 ajax 表单提交

标签 jquery ajax jquery-validate

我一生都无法让它发挥作用。验证错误看起来很好,我没有收到语法错误,但什么也没有发生。表单只是提交到页面。我也无法获得成功或错误警报......

<form id="contact" class="validation" method="post" action="">
<fieldset>
<ol class="comment_fields">
    <li>
        <label for="name">Name: <span>(required)</span></label>
        <input type="text" name="name" id="name" class="required" minlength="4" tabindex="1" />
    </li>
    <li>
        <label for="email">E&ndash;Mail: <span>(required / private)</span></label>
        <input type="text" name="email" id="email" class="required email" tabindex="2" />
    </li>
    <li>
        <label for="subject">Subject: <span>(required)</span></label>
        <input type="text" name="subject" id="subject" class="required" minlength="4" tabindex="3" />
    </li>
    <li class="comment_area">
        <label for="comment">Message: <span>(required)</span></label>
        <textarea name="message" id="message" rows="8" cols="8" class="required" minlength="10" tabindex="4"></textarea>
        <cite>Please, no XHTML.</cite>
    </li>
    <li class="submit">
        <input type="submit" class="button blue" value="Send Message" id="submit" tabindex="5" />
    </li>
</ol>
</fieldset>
</form>

<script type="text/javascript">

$("#contact").validate({
    rules: {
    name: {required: true},
    email: {required: true},
    subject: {requred: true},
    submitHandler: function() {
        $.ajax({
            type: "POST",
            url: "<?php bloginfo("template_directory"); ?>/contact/process.php",
            data: formSerialize,
            timeout: 3000,
            success: function() {alert('works');},
            error: function() {alert('failed');}
        });

        return false;
    }
}
});

</script>

这是process.php:

<?php
    if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
        $name = stripslashes(strip_tags($_POST['name']));
    } else {$name = 'No name entered';}

    if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
        $email = stripslashes(strip_tags($_POST['email']));
    } else {$email = 'No email entered';}

    if ((isset($_POST['message'])) && (strlen(trim($_POST['message'])) > 0)) {
        $message = stripslashes(strip_tags($_POST['message']));
    } else {$message = 'No message entered';}

    if ((isset($_POST['subject'])) && (strlen(trim($_POST['subject'])) > 0)) {
        $subject = stripslashes(strip_tags($_POST['subject']));
    } else {$message = 'No subject entered';}

    ob_start();
?>
<html>
    <head>
        <style type="text/css"></style>
    </head>
    <body>
        <table width="550" border="1" cellspacing="2" cellpadding="2">
            <tr bgcolor="#eeffee">
                <td>Name</td>
                <td><?=$name;?></td>
            </tr>
            <tr bgcolor="#eeeeff">
                <td>Email</td>
                <td><?=$email;?></td>
            </tr>
            <tr bgcolor="#eeffee">
                <td>Message</td>
                <td><?=$message;?></td>
            </tr>
        </table>
    </body>
</html>

<?
    $body = ob_get_contents();

    $to = 'someone@example.com';
    $email = 'email@example.com';
    $fromaddress = "you@example.com";
    $fromname = "Online Contact";

    require("phpmailer.php");

    $mail = new PHPMailer();

    $mail->From     = "you@you.com";
    $mail->FromName = "Contact Form";
    $mail->AddAddress("another_address@example.com","Name 1");

    $mail->WordWrap = 50;
    $mail->IsHTML(true);

    $mail->Subject  =  $subject;
    $mail->Body     =  $body;
    $mail->AltBody  =  "This is the text-only body";

    if(!$mail->Send()) {
        $recipient = 'your_email@example.com';
        $subject = 'Contact form failed';
        $content = $body;    
        mail($recipient, $subject, $content, "From: mail@yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
        exit;
    }
?>

最佳答案

您有 submitHandler 规则内,它应该位于旁边,如下所示:

$(function() {
  $("#contact").validate({
      rules: {
          name: {required: true},
          email: {required: true},
          subject: {requred: true}
      },
      submitHandler: function(form) {
          $.ajax({
            type: "POST",
            url: "<?php bloginfo("template_directory"); ?>/contact/process.php",
            data: $(form).serialize(),
            timeout: 3000,
            success: function() {alert('works');},
            error: function() {alert('failed');}
          });
          return false;
      }
  });
});

另请注意,为了安全起见,添加了 document.ready 处理程序。

关于jQuery.validate 插件和 ajax 表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4198816/

相关文章:

jquery - 添加og : meta info for Facebook after Ajax load?

javascript - 使用 JQuery 在表中显示 JSON

javascript - 我在使用 jquery 加载函数时遇到问题

javascript - 如何使用带有 backbone.js 的 pubsub 聚合事件数据?

javascript - 如何防止溢出 : auto from being scrolled when the user hits the space bar? 的 <div>

php - 将值传递到外部 js 文件

javascript - AJAX XMLHTTPREQUEST 更新 MySQL 表

javascript - 点击两次即可激活? javascript onclick 问题

jQuery 复选框验证在 Bootstrap 模式内不起作用

jQuery 验证插件。向现有规则添加新规则