![](/img/trans.png)
[英]Unable to display success message after data is inserted using codeigniter php
[英]Clear form data after success codeigniter using php not jquery
我有一個注冊表。 成功后我想刪除set_value
值並取消設置所有發布的值。
視圖.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>
控制器函數.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);
}
我嘗試unset($_POST)
但沒有成功,也試試
public function clear_field_data() {
$this->_field_data = array();
return $this;
}
但沒有任何效果。 我正在使用codeigniter 3.1.5 ..請幫助解決它。
我自己解決了我的問題。
視圖.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>
控制器函數.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;
}
}
並在新版本的codeIgniter中解決了問題。
您可以刷新頁面成功:
$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);
您可以使用$this->form_validation->reset_validation();
連同$_POST = [];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.