【问题标题】:Clear form data after success codeigniter using php not jquery使用php而不是jquery成功codeigniter后清除表单数据
【发布时间】:2017-11-26 18:07:45
【问题描述】:

我有一个注册表。成功后我想删除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>

控制器函数.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 forms codeigniter-3


    【解决方案1】:

    我自己解决了我的问题。

    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>
    

    控制器函数.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解决了问题。

    【讨论】:

    • 你的答案对我有用,但是,我有一个问题,你怎么能添加一个新的表单验证,我不能那样做,我必须把函数放在 CI_Form_validation 中
    • $_POST = array();这就是我所缺少的......谢谢
    【解决方案2】:

    成功后可以刷新页面:

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

    【讨论】:

    • 我试过这个redirect(current_url(), 'refresh');,因为我在一个子领域工作,但它没有显示成功消息。
    • 同样使用会话闪存。 $this-&gt;session-&gt;set_flashdata('success',"Your message");codeigniter.com/user_guide/libraries/sessions.html#flashdata
    • 这不是解决我的问题的好方法。因为我想清空set_value 字段。我知道我可以重定向并且可以获得空字段。
    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 2019-06-07
    • 1970-01-01
    • 2020-07-25
    • 2013-01-10
    • 1970-01-01
    • 2021-01-23
    • 2014-06-16
    相关资源
    最近更新 更多