jQuery.validate插件和Ajax表单提交表单、插件、jQuery、validate

2023-09-10 16:33:36 作者:该用户已诈尸

我不能为我的生命得到这个工作。验证错误出现很好,我不会有语法错误,但没有任何反应。表单只是提交到页面。我不能得到成功或错误警报的工作要么...

 <形式ID =接触级=验证的方法=邮报行动=>
<字段集>
<醇类=comment_fields>
    <李>
        <标签=名与GT;名称:<跨度>(必填)LT; / SPAN>< /标签>
        <输入类型=文本名称=名称ID =名级=需要MINLENGTH =4的tabindex =1/>
    < /李>
    <李>
        <标签=电子邮件> E&安培; ndash的;邮件:<跨度>(必填/私人)LT; / SPAN>< /标签>
        <输入类型=文本名称=电子邮件ID =电子邮件级=必须电子邮件的tabindex =2/>
    < /李>
    <李>
        <标签=主题>主题:<跨度>(必填)LT; / SPAN>< /标签>
        <输入类型=文本名称=主题ID =主题类=需要MINLENGTH =4的tabindex =3/>
    < /李>
    <李类=comment_area>
        <标签=意见>消息:<跨度>(必填)LT; / SPAN>< /标签>
        < textarea的名字=消息ID =消息行=8COLS =8级=需要MINLENGTH =10的tabindex =4>< / textarea的>
        <举>请,没有XHTML< /举>
    < /李>
    <李类=提交>
        <输入类型=提交级=按钮,蓝色的值=发送消息ID =提交的tabindex =5/>
    < /李>
< / OL>
< /字段集>
< /形式GT;

<脚本类型=文/ JavaScript的>

$(#联系人)。验证({
    规则:{
    名称:{要求:真实},
    电子邮件:{要求:真实},
    主题:{requred:真},
    submitHandler:函数(){
        $阿贾克斯({
            键入:POST,
            网址:< PHP代码bloginfo?(template_directory);,;&GT /contact/process.php?
            数据:formSerialize,
            超时:3000,
            成功:函数(){警报(作品);},
            错误:函数(){警报(失败);}
        });

        返回false;
    }
}
});

< / SCRIPT>
 

这是process.php:

 < PHP
    如果((使用isset($ _ POST ['名称']))及及(strlen的(修剪($ _ POST ['名称']))0)){
        $名=函数stripslashes(用strip_tags($ _ POST ['名称']));
    }其他{$名称='没有名字进入';}

    如果((使用isset($ _ POST [电子邮件]))及及(strlen的(修剪($ _ POST [电子邮件]))0)){
        $电子邮件=函数stripslashes(用strip_tags($ _ POST [电子邮件]));
    }其他{$电子邮件='没有电子邮件进入';}

    如果((使用isset($ _ POST ['消息']))及及(strlen的(修剪($ _ POST ['消息']))0)){
        $消息=函数stripslashes(用strip_tags($ _ POST ['消息']));
    }其他{$消息='没有消息进入';}

    如果((使用isset($ _ POST ['主题']))及及(strlen的(修剪($ _ POST ['主题']))0)){
        $主题=函数stripslashes(用strip_tags($ _ POST ['主题']));
    }其他{$消息='没有主题输入';}

    ob_start()函数;
?>
< HTML>
    < HEAD>
        <风格类型=文本/ CSS>< /风格>
    < /头>
    <身体GT;
        <表格的宽度=550的边界=1CELLSPACING =2的cellpadding =2>
            &所述; TR BGCOLOR =#eeffee>
                < TD>名称< / TD>
                < TD>< = $名称;?>< / TD>
            < / TR>
            &所述; TR BGCOLOR =#eeeeff>
                < TD>电子邮件< / TD>
                < TD>< = $电子邮件;?>< / TD>
            < / TR>
            &所述; TR BGCOLOR =#eeffee>
                &其中; TD>消息和所述; / TD>
                < TD>< = $信息;?>< / TD>
            < / TR>
        < /表>
    < /身体GT;
< / HTML>

&LT ;?
    $身体= ob_get_contents();

    $到=someone@example.com;
    $电子邮件='email@example.com';
    $ fromaddress =you@example.com;
    $ fromname =在线联系;

    要求(phpmailer.php);

    $电子邮件=新的PHPMailer();

    $ MAIL-肽从=you@you.com;
    $ MAIL-> FromName =联系表;
    $ MAIL-> AddAddress(another_address@example.com,名称1);

    $ MAIL->自动换行= 50;
    $ MAIL-> IsHTML(真正的);

    $ MAIL->主题= $主题;
    $ MAIL->身体= $机构;
    $ MAIL-> AltBody =这是纯文本的身体;

    如果($ MAIL->!发送()){
        $收件人='your_email@example.com';
        $主题=联系表格失败;
        $内容= $机构;
        邮件($收件人,$主题,$内容,从:mail@yourdomain.com \ r \ nReply-到:$电子邮件\ r \ NX-梅勒:DT_formmail);
        出口;
    }
?>
 

解决方案 jquery.validate插件表单验证问题

submitHandler 的在的规则,应该是旁边的是,像这样的:

  $(函数(){
  $(#联系人)。验证({
      规则:{
          名称:{要求:真实},
          电子邮件:{要求:真实},
          主题:{requred:真}
      },
      submitHandler:功能(形式){
          $阿贾克斯({
            键入:POST,
            网址:< PHP代码bloginfo?(template_directory);,;&GT /contact/process.php?
            数据:$(形式).serialize()
            超时:3000,
            成功:函数(){警报(作品);},
            错误:函数(){警报(失败);}
          });
          返回false;
      }
  });
});
 

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

I cannot for the life of me get this to work. The validation errors appear fine, I don't get syntax errors but nothing happens. The form just submits to the page. I can't get the success or error alerts to work either...

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

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

解决方案

You have submitHandler within rules, it should be beside it, like this:

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

Also note the addition of the document.ready handler to be safe.