简体   繁体   English

使用 AJAX 在 CodeIgniter 中进行表单验证

[英]Form validation in CodeIgniter using AJAX

I have one form and fields are Fullname, Password and Mobile no and each field has the single button.我有一个表格,字段是全Fullname, Password and Mobile no ,每个字段都有一个按钮。 All the fields are displaying single in the page.所有字段在页面中都显示为单个。 If the user clicked on the button then next field will display but I have to set the validation on it using AJAX.如果用户单击按钮,则将显示下一个字段,但我必须使用 AJAX 对其设置验证。 I have to display the error single on each field.我必须在每个字段上显示错误单。 Would you help me in this?你会帮我吗?

I tried below code but I am getting false output in the alert.我尝试了下面的代码,但我在警报中得到了错误的输出。

My controller我的控制器

public function submit_from(){
        $this->load->library('form_validation');
        $this->load->helper('form');
        $this->form_validation->set_error_delimiters('', '');
      $this->form_validation->set_rules('fullname', 'fullname', 'required|min_length[5]|max_length[20]|trim|xss_clean');
      $this->form_validation->set_rules('password', 'password', 'required|min_length[5]|max_length[20]|trim|xss_clean');
      $this->form_validation->set_rules('mobile', 'mobile', 'required|min_length[5]|max_length[20]|trim|xss_clean');

        if ($this->form_validation->run() == FALSE)
        {
         echo validation_errors();
        }
        else
        {
        echo "true";

        }
}

View看法

<!DOCTYPE html>
<html>
<head>
    <title></title>
     <style type="text/css">
    #password_form, #mobile_form{
      display: none;
    }
  </style>
</head>
<body>

<form class="active_form" name="form_1" method="post">
    <div id="name_form">
   <!--Name form********************************************************-->
      <label>Full name</label>
      <input type="text" name="fullname" id="fullname" placeholder="Full name">
      <?php echo form_error('fullname'); ?>
      <button type="button" id="continue_to_password">Continue to Password</button>
   </div>
   <!--password form********************************************************-->
   <div id="password_form">
      <label>Password</label>
      <input type="password" name="password" id="password" placeholder="password name">
      <?php echo form_error('password'); ?>
      <button type="button" id="continue_to_mobile">Continue to mobile no</button>

   </div>
   <!--mobile form********************************************************-->
   <div id="mobile_form">
      <label>Mobile number</label>
      <input type="text" name="mobile" id="mobile" placeholder="mobile no">
      <?php echo form_error('mobile'); ?>
      <button type="submit">Submit</button>
   </div>
</form>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">

$(function () {
$('form[name="form_1"]').on('submit', function (e) {
e.preventDefault();
        $.ajax({
            type: 'post',
            url: '<?php echo base_url("index.php/testcontroller/submit_from"); ?>',
           data: $('form[name="form_1"]').serialize(),
            success: function (data) {
                alert(data);
            }
        });
    });
});

/*When clicked on button*/
$('body').on('click', '#continue_to_password', function(e) {
  $('#name_form').hide();
  $('#password_form').show();
});
$('#continue_to_mobile').on('click', function() {

  $('#password_form').hide();

  $('#mobile_form').show();
});
</script>
</body>
</html>

I tried client side validation using Jquery but this is also working at the end when I clicked on submit button.我尝试使用 Jquery 进行客户端验证,但是当我单击提交按钮时,这在最后也有效。

Jquery查询

$(document).ready(function() {
    $(".active_form").validate({
        rules: {
            fullname: {
                required: true,
                 minlength:3,
                maxlength:50
            },

            password: {
                required: true,
                 minlength:3,
                maxlength:50
            },
            mobile: {
                required: true,
                 minlength:3,
                maxlength:50
            }

        },
    })

    $('#continue_to_password').click(function() {
        $(".active_form").valid();
    });
});

You may see the result of your validation:您可能会看到验证结果:

if ($this->form_validation->run() == FALSE) {
    echo validation_errors();
} 

Please see this post it may help you... Do form validation with jquery ajax in codeigniter请参阅这篇文章,它可能对您有所帮助... 在 codeigniter 中使用 jquery ajax 进行表单验证

For validation using jQuery with ajax submit you can try this script.要使用带有 ajax 提交的 jQuery 进行验证,您可以尝试使用此脚本。

jQuery(function($){

        $(".active_form").validate({
          rules: {
              fullname: {
                required: true,
                 minlength:3,
                maxlength:50
            },

            password: {
                required: true,
                 minlength:3,
                maxlength:50
            },
            mobile: {
                required: true,
                 minlength:3,
                maxlength:50
            }
          },
          submitHandler: function (form) {

                var request;
                // bind to the submit event of our form

                // let's select and cache all the fields
                var $inputs = $(".active_form").find("input, select, button, textarea");
                // serialize the data in the form
                var serializedData = $(".active_form").serialize();

                //alert(serializedData);

                // let's disable the inputs for the duration of the ajax request
                $inputs.prop("disabled", true);

                request = $.ajax({
                        url: "http://ajax/function/url/here",
                        type: "POST",
                        data: serializedData,
                });

                // callback handler that will be called on success

                request.done(function(data) {

                        // log a message to the console
                        alert("success awesome");

                });
                request.fail(function (jqXHR, textStatus, errorThrown) {
                        // log the error to the console

                });
                request.always(function () {
                        // reenable the inputs
                        $inputs.prop("disabled", false);
                });
            }
      });

    });

Finally, I found My solution.最后,我找到了我的解决方案。 I don't know it's the correct way to do but it's solved my issue.我不知道这是正确的做法,但它解决了我的问题。

$(document).ready(function() {
    $("form[name='form_1']").validate({
        rules: {
            fullname: {
                required: true,
                 minlength:3,
                maxlength:50
            },

            password: {
                required: true,
                 minlength:3,
                maxlength:50
            },
            mobile: {
                required: true,
                 minlength:3,
                maxlength:50
            }

        },
    })

   $('body').on('click', '#continue_to_password', function(e) {
        if($("form[name='form_1']").valid())
        {
            $('#name_form').hide();
            $('#password_form').show(); 
        }
    });

   $('#continue_to_mobile').on('click', function() {
     if($("form[name='form_1']").valid()){
          $('#password_form').hide();
          $('#mobile_form').show();
            }
});

});

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

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