简体   繁体   English

表单提交代码初始化后保护值

[英]Protect values after form submission codeigniter

What I am trying to do is show the form validation errors if there are any and prepopulcate the form. 我正在尝试做的是显示表单验证错误(如果存在)并预填充表单。

View 视图

<input type="hidden" value="<?php echo $_GET['project_id']; ?>"  name="project_id" id="project_id">
<input type="text" value="<?php echo set_value('name'); ?>"  name="name" id="name">

Controller 控制者

$validation_rules = $this->access_m->rules;
        $this->form_validation->set_rules($validation_rules);
        if($this->form_validation->run()){
            // process the form

        }else{


            $data= array(
                    'subview'=>'access/access',
                );
            $this->load->view('layout', $data, FALSE);
        }

Now what the problem is, I can prepopulate the value for the name, but how do I prepopulate the value for the project_id field. 现在的问题是,我可以预填充名称的值,但是如何预填充project_id字段的值。 Since the value for the field is extracted from the GET method, is there any way I can pass it back to the view so that I can use it again. 由于该字段的值是从GET方法中提取的,因此有什么方法可以将其传递回视图,以便再次使用它。

I could use a redirect and add the project ID to the url as example.com/access?project_id=23 but then I would loose the values to prepopulate to the rest of the form fields. 我可以使用重定向并将项目ID作为example.com/access?project_id=23添加到url中,但随后我将松散这些值以将其预填充到其余的表单字段中。

Any solutions ?? 任何解决方案??

Create method that accepts parameters example.ufo/controller/accept/<id> 创建接受参数example.ufo/controller/accept/<id>

From now on you don't need hidden field, your id is accesible within the method more info is here http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri 从现在开始,您不需要隐藏字段,您的ID即可在该方法内访问。有关更多信息,请参见http://ellislab.com/codeigniter/user-guide/general/controllers.html#passinguri

example: 例:

<?php
class User extends CI_Controller {

    public function accesible( $id = false) {
        //necessary checks, is id valid? is it numeric? is id in database?... does user have permission to work with element with this ID?

        echo $id;
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM