douchensi8625 2016-04-05 12:42
浏览 30
已采纳

表单验证运行false时返回输入值

View

<div class="box-body">
    <div class="form-group">
        <label for="FNAME" class="col-sm-2 control-label col-sm-offset-2">  <span>*</span>First Name:</label>
        <div class="col-sm-5">
            <input type="text" class="form-control" id="FNAME" name="FNAME">
        </div>
    </div>
    <div class="form-group has-error col-sm-offset-7">
        <label class="control-label" for="error"><?php echo form_error("FNAME"); ?></label>
    </div>
</div>

Controller

public function create_id() {

    $this->form_validation->set_rules('FNAME', 'First Name'         ,'trim|required|max_length[30]');
    $this->form_validation->set_rules('MNAME', 'Middle Name'            ,'trim|max_length[30]');
    $this->form_validation->set_rules('SURNAME', 'Last Name'                ,'trim|required|max_length[30]');

    if($this->form_validation->run($this) == FALSE) {
        $this->add_view();
    } else {
        if($query = $this->Employees_Model->insert()) {
            $this->autoid();
            redirect('Employees/index');
        } else {
            $this->add_view();
        }
    }   
}

What I want is if($this->form_validation->run($this) == FALSE){as you see it will redirect back to view. $this->add_view(); all I want is when it redirects back, the data that I input will remain. so that I will not input it again when the validation fails.

  • 写回答

3条回答 默认 最新

  • dqkx69935 2016-04-05 13:08
    关注

    As per CodeIgniter documentation, you need to alter your view file a little bit, in the means of, printing those input element values with <?= set_value("FNAME"); ?> in the input element _value_ parameter. So your

    <input type="text" class="form-control" id="FNAME" name="FNAME">
    

    would become

    <input type="text" class="form-control" id="FNAME" name="FNAME" value="<?= set_value("FNAME"); ?>">
    

    and so on. This way you will tell CodeIgniter to re-populate the form after an error.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?