jquery - codeigniter form validation is not working -


i have tried several times according ci user guide if there empty text fields process works same fields filled. problem how can echo validation errors near individual input fields,

here code

view

<div id="content">      <h2>bank account details.</h2>     <?php           $this->load->helper('form');           $attributes =  array('method'=>'post','name'=>'create_bank','id'=>'create_bank');           echo form_open_multipart('',$attributes);?>      <label>account number : </label> <?php echo form_input('accountnumber');?><br/> <br/>     <label>bank : </label> <?php echo form_input('bank');?><br/><br/>     <?php echo form_hidden('branch',$id);?><br/><br/>     <input type="submit" name="submit" value="save"/>     <?php echo form_close(); ?>   </div>  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">   </script>     <script>   //no need specify language        $(document).ready(function() {         $('#create_bank').on("submit",function(e) {              e.preventdefault();              $.ajax({                 type: "post",                 url: "<?php echo site_url('site/create_bank'); ?>",                 data: $(this).serialize(),                 success: function(data){                     //var site_url = "<?php echo site_url('site/home'); ?>";                     window.location.href = "<?php echo site_url('site/home'); ?>";                 }            });                     });       });     </script> 

controller

function create_bank(){      $this->load->library('form_validation');     $this->form_validation->set_rules('accountnumber', 'account number', 'required');     $this->form_validation->set_rules('bank', 'bank', 'required');      if ($this->form_validation->run() == false)     {         $this->home();     }     else     {         $this->load->model('bank_account_model');         $this->bank_account_model->insert_bank();     }    } 

in order view error messages page should refreshed below code works see full validation errors:

<?php echo validation_errors() ?> 

you can put code above form. here submitting form via ajax , in case need collect errors in variable pass view page , show errors singe variable. check below code

function create_bank(){     $this->load->library('form_validation');     $this->form_validation->set_rules('accountnumber', 'account number', 'required');     $this->form_validation->set_rules('bank', 'bank', 'required');      if ($this->form_validation->run() == false)     {         $data['validationerrors']=validation_errors(); //errors collected         $this->home($data); //passed home, can define function public function home($msg=''){}     }     else     {         $this->load->model('bank_account_model');         $this->bank_account_model->insert_bank();     }  } 

and on view page, add below line of code above form

<?php if(isset($validationerrors)&&($validationerrors!='')) {    echo $validationerrors;  //this variable has been passed home(via create_bank). } ?> 

now if want individual errors captured, below line of code gives individual error messages:

<?php echo form_error('fieldname')?> 

so capture individual errors in separate variables , show where.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -