【问题标题】:Form validation in CodeIgniter using AJAXCodeIgniter 中的表单验证使用 AJAX
【发布时间】:2017-09-28 11:38:21
【问题描述】:

我有一个表单,字段是Fullname, Password and Mobile no,每个字段都有一个按钮。所有字段在页面中都显示为单个。如果用户单击按钮,则会显示下一个字段,但我必须使用 AJAX 对其进行验证。我必须在每个字段上显示单个错误。你会在这方面帮助我吗?

我尝试了下面的代码,但在警报中得到了错误的输出。

我的控制器

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

        }
}

查看

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

我尝试使用 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();
    });
});

【问题讨论】:

  • 您尚未拆分验证过程。到目前为止,您尝试过什么来自己解决这个问题?另外,考虑到现在,服务器所做的只是执行长度检查,为什么不做这个客户端呢?
  • @ChrisG,无论我尝试过什么,我都会在这里上传。
  • @ChrisG,我尝试了客户端,但是当我点击提交按钮时这也在最后工作

标签: javascript php jquery ajax codeigniter


【解决方案1】:

您可能会看到验证结果:

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

请看这篇文章,它可能对你有所帮助... Do form validation with jquery ajax in codeigniter

【讨论】:

  • 是的,Tom 先生,现在我在警报中收到错误消息,但是如何逐个显示?因为每个字段都有按钮
  • 最后点击提交按钮后显示错误。
【解决方案2】:

要使用带有 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);
                });
            }
      });

    });

【讨论】:

    【解决方案3】:

    最后,我找到了我的解决方案。我不知道这是正确的做法,但它解决了我的问题。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-28
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多