使用codeigniter

时间:2016-07-07 07:01:59

标签: php ajax codeigniter

在此,当我提交我的表单时,它首先显示错误,然后重新加载页面并转到索引页面..但我想要显示只有错误信息后没有重新加载页面..这里我的控制器是       

类用户扩展CI_Controller {

public function index()
{

     $this->load->view('index');


}
public function error()
{
    /* Load form helper */ 
     $this->load->helper(array('form'));

     /* Load form validation library */ 
     $this->load->library('form_validation');

     /* Set validation rule for name field in the form */ 
    $form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name'), 'mobile_no' => $this->input->post('Mobile No.'));         
     if($this->form_validation->run($form_data) == FALSE )
     { 

      echo validation_errors();

     } 
     else 
     { 
       $this->load->view('submit'); 
    } 
}
}

我的名为index.php的视图文件是

        
    <script type="text/javascript">
    $(document).ready(function(){
    $('#get').click(function(){
    $.post("<? base_url() . 'user/error' ?>",
    $("form").serialize(),
    function(result) {
    $("#error_message").html(result).appendTo("form"); 
    },"html");
    });    
    });
    </script>
    <form role="form" action="" class="form-inline" name="form" method="POST" id >
    <div id="error_message" style="color:red"></div>

     <?php echo form_open('form'); ?> 
            <div class="col-md-3">
            </div>
            <div class="col-md-2">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Brand Name" name="brand_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Brand Name'">
                </div>
            </div>
            <div class="col-md-2">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Your Name" name="your_name" onfocus="this.placeholder = ''" onblur="this.placeholder='Your Name'">
            </div>
            </div>

            <div class="col-md-2">
            <div class="form-group">                
                <input type="text" class="form-control" placeholder="Mobile No" name="mobile_no" onfocus="this.placeholder = ''" onblur="this.placeholder='Mobile No'">
            </div>  
            </div>
            <div class="col-md-2">
            <div class="form-group">                
                <input type="submit" id="get" class="btn btn-success" name="submit" value="Proceed" ></input>
            </div>  
            </div>
            <div class="col-md-3">
            </div>
        </form>

我只是希望它显示错误信息,只有在请帮助后才重新加载页面...提前感谢

1 个答案:

答案 0 :(得分:0)

您正尝试在AJAX中发布表单,因此您需要手动设置表单数据

使用CI 2进行测试

$form_data = array('brand_name' => $this->input->post('brand_name'), 'your_name ' => $this->input->post('your_name')); // other fields as required for validations

同样的验证将是

if($this->form_validation->run($form_data) == TRUE ){
     //validation passed
}else{
     //validation fail
}

对于CodeIgniter 3,正如在线文档中提到的那样尝试https://www.codeigniter.com/userguide3/libraries/form_validation.html#validating-an-array-other-than-post

验证数组($ _POST除外) 有时您可能希望验证不是源自$ _POST数据的数组。

在这种情况下,您可以指定要验证的数组:

$data = array(
        'username' => 'johndoe',
        'password' => 'mypassword',
        'passconf' => 'mypassword'
);

$this->form_validation->set_data($data);