【问题标题】:jQuery.validate plugin and ajax form submissionjQuery.validate 插件和 ajax 表单提交
【发布时间】:2010-11-16 20:52:07
【问题描述】:

我这辈子都无法让它发挥作用。验证错误看起来很好,我没有收到语法错误,但没有任何反应。表单只是提交到页面。我也无法让成功或错误警报正常工作...

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

【问题讨论】:

  • hrmm 有些被 process.php 切断了...

标签: jquery ajax jquery-validate


【解决方案1】:

你有submitHandler 在 rules,它应该在旁边它,像这样:

$(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 处理程序以确保安全。

【讨论】:

  • 不错的收获。这种血腥的缩进让人很难辨认。
  • @Chris - 定义“不工作”?您是否看到任何验证错误?
  • 我收到验证错误,但表单没有给出成功警报。它只是提交给自己。
  • 这里有一个问题,如果它在本地机器上的 mamp 上运行有关系吗?
  • 好的,现在在 Web 服务器上,它显然找不到 process.php,出现 404 错误。但是如果我输入在源代码中找到的URL process.php 可以找到...
【解决方案2】:

只是为了帮助更新这篇文章。

封装:

$(document).ready(function() { ALL YOUR CODE GOES HERE }

从规则中删除 submitHandler:

rules: {
          name: {required: true},
          email: {required: true},
          subject: {requred: true}
      },
submitHandler: function(form) {.....

添加缓存:false,帮助防止浏览器表单返回缓存内容:

request = $.ajax({
    type: "POST",
    cache: false,
    url: "<?php bloginfo("template_directory"); ?>/contact/process.php",
    data: $(form).serialize(),
    timeout: 3000
});

现在使用 done() 和 fail() 代替成功和错误:

// Called on success.
request.done(function(msg) {
    alert('works');
});
// Called on failure.
request.fail(function (jqXHR, textStatus, errorThrown){
    alert('failed');
    // log the error to the console
    console.error(
        "The following error occurred: " + textStatus, errorThrown
    );
});

这就是全部内容:

$(document).ready(function() {
    $("#contact").validate({
        rules: {
            name: {required: true},
            email: {required: true},
            subject: {requred: true}
        },
        submitHandler: function(form) {
            request = $.ajax({
                type: "POST",
                cache: false,
                url: "<?php bloginfo("template_directory"); ?>/contact/process.php",
                data: $(form).serialize(),
                timeout: 3000
            });
            // Called on success.
            request.done(function(msg) {
                alert('works');
            });
            // Called on failure.
            request.fail(function (jqXHR, textStatus, errorThrown){
                alert('failed');
                // log the error to the console
                console.error(
                    "The following error occurred: " + textStatus, errorThrown
                );
            });
            return false;
        }
    });
});

在 process.php 标头中添加 no-cache 以帮助防止浏览器表单缓存内容:

<?=header("cache-control: no-cache");?>

【讨论】:

    【解决方案3】:

    将您的 submitHandler 排除在规则之外 :-)

    $("#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;
                            }
                        });
    

    【讨论】:

      猜你喜欢
      • 2010-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 2013-12-30
      相关资源
      最近更新 更多