简体   繁体   English

使用jQuery Validate插件保持在页面上

[英]Stay on the page with jQuery Validate plugin

When I click on the submit button of my form I'm redirected because of the PHP on the page "Form sent". 当我点击我的表单的提交按钮时,由于页面上的PHP“已发送表单”,我被重定向。 How to stay in the same page with jQuery validate form plugin please ? 如何与jQuery验证表单插件保持在同一页面?

PHP FILE PHP文件

    <?php
        // [I HAVE CUT THE CODE]

        if(mail($to, $subject, $message, $headers)){
            echo "Form sent";
        } else {
            echo "Form not sent";
        }
    ?>

JQUERY FILE JQUERY FILE

$("#form-contact").validate({

submitHandler: function(form) {
            $('.btn-validate-group:visible').hide();
            $('.message-success').fadeIn(1000);
            form.submit();
        }

});

HTML FILE HTML文件

<form id="form-contact" action="php/traitement.php" method="post"></form>

UPDATE 1 更新1

submitHandler: function(form) {
    $('.btn-validate-group:visible').hide();
    $('.message-success').fadeIn(1000);
    $.ajax({
          type: "POST",
          url: url,
          data: data,
          success: function(result){ console.log("success!", result);},
          dataType: dataType
        });
    return false;
}

I'm always redirected on "Form sent" page. 我总是在“表格发送”页面上重定向。 I know nothing about Ajax :-/ 我对Ajax一无所知: - /

UPDATE 2 更新2

http://jsfiddle.net/Xroad/2pLS2/25/ http://jsfiddle.net/Xroad/2pLS2/25/

If I understand correctly what you want, one way would be to try to submit from jQuery. 如果我理解你想要什么,一种方法是尝试从jQuery提交。 In the submitHandler have something like: 在submitHandler中有类似的东西:

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: function(result){ console.log("success!", result);},
  dataType: dataType
});

The tricky part would be to get all the information into the data object before calling this. 棘手的部分是在调用之前将所有信息都放入数据对象中。

The success function would have the data from the server after posting. 成功函数将在发布后从服务器获取数据。

More info: https://api.jquery.com/jQuery.post/ 更多信息: https//api.jquery.com/jQuery.post/

jQuery .ajax() can be used to submit data to the server without a page refresh, but it's not exclusive to the jQuery Validate plugin. jQuery .ajax()可用于在没有页面刷新的情况下向服务器提交数据,但它并不是jQuery Validate插件所独有的。


However, here are your two options using the jQuery Validate plugin. 但是,这是使用jQuery Validate插件的两个选项。

Standard form submit using the default action of the form element (as you've done)... 标准表单使用form元素的默认action提交(如您所做)...

$("#form-contact").validate({
    submitHandler: function(form) {
        $('.btn-validate-group:visible').hide();
        $('.message-success').fadeIn(1000);
        form.submit();  // standard submit of the default form action
    }
});

To stay on same page, use .ajax() in the submitHandler callback... 要保持同一页面,请在submitHandler回调中使用.ajax() ...

$("#form-contact").validate({
    submitHandler: function(form) {
        $('.btn-validate-group:visible').hide();
        $('.message-success').fadeIn(1000);
        $.ajax({    // submit form using ajax
            // your ajax options here
        });
        return false;  // block default form action  
    }
});

See the jQuery .ajax() documentation for the options . 有关选项,请参阅jQuery .ajax()文档


This is your own jsFiddle , which shows everything is working. 这是你自己的jsFiddle ,它显示一切正常。 I cannot test the ajax but the form is not refreshing the page as you claim. 我无法测试ajax但表单并未像您声明的那样刷新页面。

If you make it work without validate plugin and more organised validation process then I would like you to have a look my code: 如果你没有验证插件和更有条理的验证过程使它工作,那么我希望你看看我的代码:

  jQuery(document).ready(function($) {

     $('#MyForm').on('submit', function(){
        var form = this;
        if(validateForm(form)) {
            var values = $(form).serialize();
            $.ajax({
                url: "test.php",
                type: "post",
                data: values ,
                success: function (response) {
                    // you will get response from your php page (what you echo or print)
                },
                error: function(jqXHR, textStatus, errorThrown) {
                    console.log(textStatus, errorThrown);
                }


            });
            event.preventDefault(); //changed to allow the tag manager to notice that the form was submitted
        }
        else{
            event.preventDefault();
            return false;
        }
      });

        // validate Form
        function validateForm(form) {
            valid = true;

            $(form).find('input[type=text], input[type=email]').each(function(i, val){
                if(validateField(val, true) == false) { valid = false; }
            });

            return valid;
        }

        // validate all form fields
        function validateField(field, submit) {
            var val = $(field).val();
            if($(field).attr('aria-required') == 'true' && submit){
                if(val == '') {
                    $(field).parent().removeClass('valid');
                    $(field).parent().addClass('error');
                    return false;
                }else {
                    $(field).parent().removeClass('error');
                    $(field).parent().addClass('valid');
                    return true;
                }
                // you can more specific
                if($(field).attr('type') == 'text') {
                    $(field).parent().addClass('error');
                    return false; }
                else {
                    $(field).parent().removeClass('error');
                    $(field).parent().addClass('valid');
                    return true;
                }
                // you can more specific
                if($(field).attr('type') == 'email') {
                    $(field).parent().addClass('error');
                    return false; }
                else {
                    $(field).parent().removeClass('error');
                    $(field).parent().addClass('valid');
                    return true;
                }
            }
        }
        // Run validation before Submit the form
        $('input[type=text], input[type=email]').on('change', function(){
            if($(this).val() != ''){
                $(this).parent().removeClass('error valid');
                validateField(this, false);
            }
        });
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM