SubmitHandler和.validate()问题 [英] submitHandler and .validate() issue

查看:69
本文介绍了SubmitHandler和.validate()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在将文件附加到电子邮件时遇到问题,我已经解决了,谢谢大家.现在,我遇到了与修复"附件相关的下一个问题. 其实有比我想的更多的问题. 1)在下面的代码中,在验证插件中,我认为足以调用诸如"name:requred"之类的规则,但事实并非如此.为了使其正常工作,我必须以class ="required"的形式进行调用,为什么呢?当我删除class ="requred"时,验证不再存在.

recently i had problem with attaching file into email, i handle that, thanks for you guys btw. Now i have next problem connected with "fixed" attching the file. Actually there is more problems than i thought. 1) in code below, in validation plugin, i thought that it's enought to call in rules somenthing like this "name : requred", but it isn't. To make it works i have to call in form class="required", why ? When i remove class="requred" validation is no longer exist.

2)好吧,所以class ="required",小问题,验证工作正常,但是submitHandler发生了什么?阿贾克斯没有运行,网站刷新,我得到成功的消息.我的观点是发送带有附件的电子邮件,而无需刷新.

2) Ok, so class="required", small problem, validation is working, but what happend with submitHandler ? ajax doesnt run, website refresh and i get succes message. My point is to send email with attachment without refreshig.

3)更糟糕的是,我写到upload_file规则的消息"没有出现,该消息应该在未上传文件时显示,并没有插入title属性(形式)中的消息.

3) to make matters worse, the "message" that i wrote to uploaded_file rule, which should show when file is not uploaded, doesnt appear, insted the message from title attribute (in form) come into.

所以一切都不是我应有的.我应该修理/修理/更改什么才能使其正常工作?请帮助我

so everything is not that i was supposed to by. What shoud i repair/fix/change to make it work? please help me

代码: 形式:

 <form method="post" name="formularzaplikacyjny" enctype="multipart/form-data" action="mail-attachment.php" id="formmail"> 

        <div id="imiediv"><label for="name">Imię i nazwisko: <em>*</em> </label><br>
                        <input type="text" name="name" id="name" class="required" title="Wpisz swoje imię i nazwisko" placeholder="Jan Kowalski"></div><br>

        <div id="emaildiv"><label for="email">Email: <em>*</em> </label><br>
                        <input type="text" name="email" class="required" id="email"  title="Wpisz swój adres email" placeholder="twoj_adres_email@email.com"></div><br>

        <div id="listdiv"><label for="message">List motywacyjny: <em>*</em></label><br>
                        <textarea name="message" rows="5" cols="48" class="required" id="message" title="Wpisz treść listu motywacyjnego"  placeholder="Tutaj zpowinna znaleźć się treść Twojego listu motywacyjnego" ></textarea></div>

        <div id="cvdiv"><label for="uploaded_file">Wybierz plik CV: <em>*</em></label><br>
                        <input type="file" name="uploaded_file" title="<h3>Wybierz plik CV do przesłania</h3>" class="required" id="uploaded_file"></div><br>

                        <input type="submit" value="Prześlij" name="submit" id="submitbutton">
                        </form>

                        <div id="loading-mail">
                            <h2>Wysyłamy maila.....</h2>
                          </div>

验证:

$("#formmail").validate({

   rules: {
     email: {
        required: true,
        email: true
     },
      name: {
        required: true

     }, 
     message: {
        required: true
     },
     uploaded_file: {
        requred: true
     }
   }, //koniec literału obiektowego rules
   messages: {
      email: {
         required: "<h3>Podaj adres e-mail.</h3>",
         email: "<h3>To nie jest prawidłowy <br>adres e-mail.</h3>"
       },
       name: {
         required: "<h3>Podaj swoje imię i nazwisko.</h3>"
       },
      message: {
        required: "<h3>Wpisz treść listu motywacyjnego.</h3>"
      },
      uploaded_file: {
        requred: "<h3>Prześlij plik CV</h3>"
     }
   },submitHandler: function() {


        var thisForm = $('#formmail');
        $('#formmail').fadeOut(function(){
          //Display the "loading" message
          $("#loading-mail").fadeIn(function(){
            //Post the form to the send script
            $.ajax({
              type: 'POST',
              url: thisForm.attr("action"),
              data: thisForm.serialize(),
              //Wait for a successful response
              success: function(data){
                //Hide the "loading" message
                $("#loading-mail").fadeOut(function(){
                  //Display the "success" message
                  $("#success").text(data).fadeIn();
                });
              }
            });
          });
        });
      }
    });  // koniec funkcji validate  

并发送脚本:

<?php
require "PHPMailer/class.phpmailer.php";



$mail = new PHPMailer(true); //New instance, with exceptions enabled

$mail->CharSet = "UTF-8";
$bodys="<b>Podanie od:</b> ".$_POST['name']."<br/>"."<b>Adres e-mail: </b>".$_POST['email']."<br/>"."<b>Treść listu motywacyjnego: </b><br/>".$_POST['message'];
$mail->Body =$bodys; 

$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.xxx.linuxpl.info"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "pass"; // SMTP server password

$mail->IsSendmail(); // tell the class to use Sendmail

$mail->AddReplyTo($_POST['email'],$_POST['name']);

$mail->From = $_POST['email']; //uzupełnij sobie
$mail->FromName = $_POST['name']; //uzupełnij sobie

$to = 'xxx@gmail.com'; //na jaki mail wysłać np ala@wp.pl



$mail->AddAddress($to);

$mail->Subject = "Nowe podanie o pracę";

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap

$mail->MsgHTML($bodys);



$plik_tmp = $_FILES['uploaded_file']['tmp_name'];
$plik_rozmiar = $_FILES['uploaded_file']['size'];
$plik_nazwa = $_FILES['uploaded_file']['name'];
if(is_uploaded_file($plik_tmp)) {   
$nazwa_g=$plik_nazwa;

move_uploaded_file($plik_tmp, 'tmp_zal/'.$nazwa_g); 
$mail->AddAttachment('tmp_zal/'.$nazwa_g, $nazwa_g);
}





$mail->IsHTML(true); // send as HTML




if(!$mail->Send())
{
echo "Błąd";
echo "Kod błędu: " . $mail->ErrorInfo;
}
else
{
echo 'Wiadomość została wysłana';

}

?>

推荐答案

问题1& 3是因为您在uploaded_file规则和消息中将required拼写为requred.

Issues 1 & 3 are because you misspelled required as requred in the uploaded_file rule and message.

对于问题2,您需要从submitHandler中退出return false才能取消默认的表单提交

For issue 2 you need to return false from submitHandler to cancel the default form submission

jQuery(function ($) {
    $("#formmail").validate({

        rules: {
            email: {
                required: true,
                email: true
            },
            name: {
                required: true

            }, 
            message: {
                required: true
            },
            uploaded_file: {
                required: true
            }
        }, //koniec literału obiektowego rules
        messages: {
            email: {
                required: "<h3>Podaj adres e-mail.</h3>",
                email: "<h3>To nie jest prawidłowy <br>adres e-mail.</h3>"
            },
            name: {
                required: "<h3>Podaj swoje imię i nazwisko.</h3>"
            },
            message: {
                required: "<h3>Wpisz treść listu motywacyjnego.</h3>"
            },
            uploaded_file: {
                required: "<h3>Prześlij plik CV</h3>"
            }
        },submitHandler: function() {


            var thisForm = $('#formmail');
            $('#formmail').fadeOut(function(){
                //Display the "loading" message
                $("#loading-mail").fadeIn(function(){
                    //Post the form to the send script
                    $.ajax({
                        type: 'POST',
                        url: thisForm.attr("action"),
                        data: thisForm.serialize(),
                        //Wait for a successful response
                        success: function(data){
                            //Hide the "loading" message
                            $("#loading-mail").fadeOut(function(){
                                //Display the "success" message
                                $("#success").text(data).fadeIn();
                            });
                        }
                    });
                });
            });

            return false
        }
    }); 
});

演示:小提琴

注意:由于表单中的文件输入,ajax提交将不起作用-有关其他替代方法,请参见

Note: The ajax submission will not work because of the file input in the form - for some alternates see this answer

这篇关于SubmitHandler和.validate()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆