I’m new with codeigniter. I have this form and in the end of the form the user have to re-enter his password for confirmation, however, the page reloads if the password was incorrect and all the data that the user typed on the input field disappeared. Is there a way for the validation_error to output without the page reloading?
Here’s my view form SHIFTER.HTML
<?php echo form_open('forms/submit_form');?> <div id="form-interview-fill-mainform"> <div class="container"> <div class="col-sm-6"> <div class="form-group"> <label>Strengths:</label> <textarea class="form-control form-input" onkeyup="textAreaAdjust(this)" rows="1" name="shifter_answer[]"></textarea> </div> </div> <div class="col-sm-6"> <div class="form-group"> <label>Weaknesses:</label> <textarea class="form-control form-input" onkeyup="textAreaAdjust(this)" rows="1" name="shifter_answer[]"></textarea> </div> </div> <div class="custom-form"> <?php if(validation_errors()){ ?> <div class="alert alert-danger alert-dismissible" role="alert"> <button type="button" class="close" data-dismiss="alert" aria-lable="Close"><span aria-hidden="true">×</span></button> <strong><?php echo validation_errors()?></strong> </div> <?php }?> <label>Password</label> <center> <?= form_password('password','','class="form-control" id="password" style="width:80%;font-size:20px;" required');?> </center> </div> <center><?php echo form_submit('submit','Submit Form', 'class="btn btn-success btn-lg"');?></center> </div> </div> <?php echo form_close();?>
Here’s the controller
public function submit_form(){ $this->form_validation->set_rules('password','Password','trim|required|callback_check_pass'); if ($this->form_validation->run()==false){ $this->load->view('shifter.html'); }else { $session_data = $this->session->userdata('logged_in'); $data['id'] = $session_data['id']; $givenAns= $this->input->post('shifter_answer'); foreach($givenAns as $ansKey => $ansVal){ $ansKey+=16; $ids []= $this->forms_model->shifter_form_submit($data['id'],$ansVal,$ansKey); } redirect('profile/forms','refresh'); } } function check_pass($password){ $session_data = $this->session->userdata('logged_in'); $data['id'] = $session_data['id']; $password = $this->input->post('password'); $this->load->model('forms_model'); $result = $this->forms_model->val_password($data['id'],$password); if($result){ return true; }else{ $this->form_validation->set_message('check_pass', "Invalid Password"); return false; } }
And here’s the model
function val_password($id,$password){ $this->db->select('id,password'); $this->db->from('tbl_st_account'); $this->db->where('id', $id); $this->db->where('password', $password); $this->db->limit(1); $query = $this->db->get(); if($query->num_rows()==1){ return $query->result(); }else{ return false; } }
Advertisement
Answer
The page is reloading before the server even performs the validation. You’ll need to use AJAX. AJAX the request. On failed validation, have the AJAX callback pop a message to the user. On successful validation, have the AJAX callback direct the user to the desired page.