Clear form data after success codeigniter using php not jquery

天涯浪子 提交于 2019-12-02 03:06:43

问题


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.


回答1:


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.




回答2:


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);


来源:https://stackoverflow.com/questions/44716717/clear-form-data-after-success-codeigniter-using-php-not-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!