2

I have a registration form. after success i want to remove set_value value and unset all posted value.

view.php

<?php echo validation_errors(); ?>
<?php if(isset($msg)){
     echo $msg;
} ?>
<form action="" method="post">
   <h5>Username</h5>
   <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
   <h5>Password</h5>
   <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
   <div>
       <input type="submit" value="Submit" />
   </div>
</form>

Controller function.php

 $this->load->helper(array('form', 'url'));

 $this->load->library('form_validation');
 $this->form_validation->set_rules('username', 'Username', 'required');
 $this->form_validation->set_rules('password', 'Password', 'required');
 if ($this->form_validation->run() == FALSE){
      $data->msg='Please fill all field';
      $this->load->view('view',$data);
}else{
     $data->msg='success';
     unset($_POST);
     $this->load->view('view',$data);
}

I try unset($_POST) but not worked, also try

  public function clear_field_data() {
    $this->_field_data = array();
    return $this;
  }

but nothing works. I am using codeigniter 3.1.5..Please help to sort out it.

Bhunesh Satpada
  • 770
  • 1
  • 6
  • 19

3 Answers3

5

I solved my problem my self.

view.php

<?php echo validation_errors(); ?>
<?php if(isset($msg)){
 echo $msg;
} ?>
<form action="" method="post">
  <h5>Username</h5>
  <input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
  <h5>Password</h5>
  <input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
  <div>
     <input type="submit" value="Submit" />
  </div>
</form>

Controller function.php

$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
if ($this->form_validation->run() == FALSE){
  $data->msg='Please fill all field';
  $this->load->view('view',$data);
}else{
 $data->msg='success';
 $this->form_validation->clear_field_data();
 $this->load->view('view',$data);
}

MY_Form_validation.php

class MY_Form_validation extends CI_Form_validation {

public function __construct($config)
{
    parent::__construct($config);
}

public function clear_field_data() {
    $_POST = array();
    $this->_field_data = array();
    return $this;
}
}

And problem solved in new version codeIgniter.

Bhunesh Satpada
  • 770
  • 1
  • 6
  • 19
  • your answer works for me but, i have a question, how you could add a new form validation, i could not do like that, i have to put the function inside the CI_Form_validation – Mary Aug 16 '18 at 14:28
  • $_POST = array(); this is what i was missing.. thanks – Asad Ali Khan May 24 '21 at 04:36
0

You can refresh page on success :

$this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');
        $this->form_validation->set_rules('username', 'Username', 'required');
        $this->form_validation->set_rules('password', 'Password', 'required');
        if ($this->form_validation->run() == FALSE){
                $data['msg']='Please fill all field';

        }else{
                $data['msg']='success';                
                redirect($_SERVER['REQUEST_URI'], 'refresh');                 
        }
        $this->load->view('view',$data);
B. Desai
  • 16,414
  • 5
  • 26
  • 47
  • I tried this `redirect(current_url(), 'refresh');` because i am working in a sub doamin, but it is not showing success msg. – Bhunesh Satpada Jun 23 '17 at 09:58
  • use session flash for same. `$this->session->set_flashdata('success',"Your message");` https://www.codeigniter.com/user_guide/libraries/sessions.html#flashdata – B. Desai Jun 23 '17 at 10:04
  • this is not a great way to solve my problem. because i want to empty `set_value` field. I know i can redirect and can get empty field. – Bhunesh Satpada Jun 23 '17 at 10:10
0

You can use $this->form_validation->reset_validation(); along with $_POST = [];

Rod911
  • 752
  • 5
  • 14