submitHandler and .validate() issue



最近我在将文件附加到电子邮件中时遇到问题,我处理了这个问题,顺便说一句,谢谢你们。现在我有下一个与"修复"文件相关的问题。实际上问题比我想象的要多。1)在下面的代码中,在验证插件中,我认为调用像"名称:必需"这样的规则就足够了,但事实并非如此。要使其正常工作,我必须调用表单 class="required",为什么?当我删除class="requred"验证时,验证不再存在。

2)好的,所以class="必需",小问题,验证正在工作,但是提交处理程序发生了什么?Ajax无法运行,网站刷新,我收到成功消息。我的观点是在不刷新的情况下发送带有附件的电子邮件。

3)更糟糕的是,我写给uploaded_file规则的"消息"(应在文件未上传时显示)不会出现,而是来自标题属性(表单)的消息进入。

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

代码:形式:

 <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

对于问题 2,您需要从submitHandler return false以取消默认表单提交

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 提交将不起作用 - 对于某些替代项,请参阅此答案

最新更新