在CodeIgniter中使用jQuery提交表单验证和AJAX表单

时间:2017-11-22 15:15:27

标签: jquery ajax codeigniter

当我提交表单错误显示完美但当我再次提交数据时输入数据库但是条件不工作我想隐藏表单并显示成功消息

如果密码长度小于6

,则第一次响应

密码必须大于6

第二次响应空其他条件应该有效但是如果数据成功输入则无法隐藏表单

<div class="modal-body">
 <div id="successResults"></div>
    <form method="" action="" class="form-horizontal signup-hide" role="form" id="signup-form">
        <div id="ajaxResults"></div>
           <input type="text" class="model-signup-fields"
                    name="first_name"  value="<?php echo $this->input->post('first_name');?>">  
           <input type="email" class="model-signup-fields" name="email"   value="<?php echo $this->input->post('email');?>">
          <input type="password" class="model-signup-fields" name="password"   value="<?php echo $this->input->post('password');?>">
           <input type="password" class="model-signup-fields" name="confirm_password"   value="">
                    <br>
          <input type="submit" name="remove" id="this_signup" class="claim btn btn-info model-signup "  onclick="" value="signup"  />
</form>
</div>

控制器

       public function register() {
    $data['title'] = 'Register';
    $this->load->model('auth_model');

    if (count($_POST)) {
        $this->load->helper('security');

        $this->form_validation->set_rules('first_name', 'First name', 'trim|required');
        // $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
        $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]');

        $this->form_validation->set_rules('password', 'Password', 'trim|required');
        $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[6]|matches[password]');

        if ($this->form_validation->run() == false) {
            // $data['notif']['message'] = validation_errors();
            // $data['notif']['type'] = 'danger';
             $status = validation_errors();
             if ( $this->input->is_ajax_request() ) {
                echo json_encode($status);

            }
        } 
        else {
            // $data['notif'] = $this->auth_model->register();

            $success_reg = $this->auth_model->register();



        }
    }

    if ($this->session->userdata('logged_in')) {    
        redirect(base_url('dashboard'));
        exit;
    }

    $this->load->view('includes/header', $data);

    $this->load->view('home/index');
    $this->load->view('includes/footer');
}

脚本

   $(function()
   {
      $('.model-signup').on('click', function(e){
      e.preventDefault();

      var form = $("#signup-form").serialize();

           $("#this_signup").css({"background-image":"url(<?PHP echo 
           base_url(); ?
            >assets/images/loading1.gif)",
           "background-repeat": "no-repeat",
           "background-position": "10% 50%"});
       $.ajax({
        url:"<?PHP echo base_url(); ?>auth/register",
        type:"POST",
        dataType: 'json',
        cache: false,
        data:form,
        success: function(response) {

            alert(response);

            var result2 = response;
            console.log(result2);

            if(result2)
            {   
                    alert("if");
                 $('#ajaxResults').addClass('alert alert-danger').html(result2);
                $("#this_signup").css({"background-image":"",
                    "background-repeat": "",
                    "background-position": ""});
            }
            if(response == ''){
                alert("else");
                $("#this_signup").css({"background-image":"",
                "background-repeat": "",
                "background-position": ""});
            $(".signup-hide").hide();
            $('#successResults').addClass('alert alert-success').html('You successfully register now you can login');
            }
            response='';


       }
        });
     });
 });

1 个答案:

答案 0 :(得分:0)

首先回答你的问题:在表单通过规则后,没有任何反应(Ajax请求的透视图)你提供一个状态代码为200的空白页面。回显&#34;好的&#34;而不是没有,并比较如果响应是(====)&#34;好的&#34;。

附加:重定向ajax请求不是一种正确的方法。回应&#34; alreay_logged_in&#34;作为Ajax将理解的状态代码。之后,您可以将用户重定向到仪表板。