我有一张注册表。成功后我想删除 set_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 函数.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..请帮忙解决一下。
最佳答案
我自己解决了我的问题。
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 函数.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 已解决问题。
关于php - 使用 php 而不是 jquery 成功后清除表单数据 codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44716717/