jQuery   发布时间:2022-04-19  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了jQuery.validate插件和ajax表单提交大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我不能为我的生活让这个工作.验证错误看起来很好,我没有语法错误,但没有任何反应.表单只是提交到页面.我无法获得成功或错误警报……

<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("@L_970_14@mailer.PHP");

    $mail = new @L_970_14@mailer();

    $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},subject: {requred: truE}
      },submitHandler: function(form) {
          $.ajax({
            type: "POST",data: $(form).serialize(),error: function() {alert('Failed');}
          });
          return false;
      }
  });
});

另请注意添加document.ready处理程序是安全的.

大佬总结

以上是大佬教程为你收集整理的jQuery.validate插件和ajax表单提交全部内容,希望文章能够帮你解决jQuery.validate插件和ajax表单提交所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。