简体   繁体   English

Codeigniter表单发布和验证

[英]Codeigniter form post and validation

I'm not a pro, but know my way around PHP, I'm new to Codeigniter. 我不是专业人士,但我知道我的PHP方式,我是Codeigniter的新手。

Been going through these tutorials: http://net.tutsplus.com/articles/news/codeigniter-from-scratch-day-5-crud/ 正在浏览这些教程: http//net.tutsplus.com/articles/news/codeigniter-from-scratch-day-5-crud/

OK, so I have a page that lists users, clicking on users name will go to an edit page, the url of that page being: index.php/users/edit/1 (where 1 is the users id) 好的,所以我有一个列出用户的页面,点击用户名将转到编辑页面,该页面的URL为:index.php / users / edit / 1(其中1是用户ID)

On edit page is a form, this form contains a few parts, each part is populated from different tables in the DB. 在编辑页面上是一个表单,这个表单包含几个部分,每个部分都是从DB中的不同表填充的。 So my Controller for edit is as follows: 所以我的Controller编辑如下:

function edit() {

        //load model
        $this->load->model('users_model');

        //assign user data from DB 
        $data['data_user'] = $this->users_model->getUser($this->uri->segment(3));
        //get users Password, using username from above
        $data['data_user_password']= $this->users_model->getUserPassword($data['data_user'][0]->UserName);

        $data['page_content'] = 'pages/users_edit';
        $this->load->view('template/template', $data);
    }

Notice: $data['data_user'] contains users data like name, username, email $data['data_user_password'] contains users password from a different table 注意: $data['data_user']包含用户数据,如名称,用户名,电子邮件$data['data_user_password']包含来自不同表的用户密码

I can then populate the form, on users_edit.php, this all works fine. 然后我可以填写表单,在users_edit.php上,这一切都正常。 I'm accessing this data by doing the following: 我通过执行以下操作来访问此数据:

if (is_array($data_user)) {
    foreach($data_user as $user) 
    {
        $userID         = $user->id;
        $userName       = $user->Name;
        $userUserName   = $user->UserName;
        $userMail       = $user->Mail;
        $userDepartment = $user->Department;
        $userWorkPhone  = $user->WorkPhone;
        $userHomePhone  = $user->HomePhone;
        $userMobile     = $user->Mobile;

    }
}
//user password
if (is_array($data_user_password)) {
    foreach($data_user_password as $user) 
    {
        $userPassword   = $user->value;
    }
}

Name: 
<?php echo form_input('name', set_value('name', $userName), 'id="name" class="inputLong"'); ?>

When I post, I'm sending data to: index.php/users/update 当我发布时,我将数据发送到:index.php / users / update

My controller for this page so far is: 到目前为止,我对此页面的控制器是:

function update() {

        echo '<pre>';
        print_r($_POST);
        echo '</pre>';
        //exit();

        $this->load->library('form_validation');

        $this->form_validation->set_rules('name', 'Name', 'trim|required');

        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('pages/users_edit');
        }
        else
        {
            $this->index();
        }

    }

For now, I'm just testing validation on users "name" where form input=name id=name 现在,我只是测试用户“name”的验证,其中form input = name id = name

I think I'm not handling the if ($this->form_validation->run() == FALSE) part of it correctly, if the form contains data, it passes and redirects to index, if I leave name blank it either not handling the edit page correctly, or I dont know, something isnt right.. I think its because the page is being reloaded using the post array, and not passing the $data like I did in function edit(). 我认为我没有正确处理if($ this-> form_validation-> run()== FALSE)部分,如果表单包含数据,它会传递并重定向到索引,如果我将名称留空则不正确处理编辑页面,或者我不知道,有些东西是对的..我认为它是因为页面正在使用post数组重新加载,而不像我在函数edit()中那样传递$ data。

Back to the form page, where it should be showing the validation_errors, its showing: 回到表单页面,它应该显示validation_errors,它显示:

The Name field is required. 该名称字段是必需的。

This is correct, however, for the rest of the fields that should be pre-populated, its showing PHP error: 但是,对于应该预先填充的其他字段,这是正确的,它显示PHP错误:

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: userUserName

Filename: pages/users_edit.php

Line Number: 50

You could do your validation inside your edit function instead of having an update function, that way, your data is still available for your view and if the validation has errors, codeigniter will take in charge to repopulate your fields. 您可以在编辑功能中进行验证,而不是使用更新功能,这样,您的数据仍可用于您的视图,如果验证有错误,codeigniter将负责重新填充您的字段。 If the validation is ok, you do your next step 如果验证正常,则执行下一步

function edit() {

    //load model
    $this->load->model('users_model');

    //assign user data from DB 
    $data['data_user'] = $this->users_model->getUser($this->uri->segment(3));
    //get users Password, using username from above
    $data['data_user_password']= $this->users_model->getUserPassword($data['data_user'][0]->UserName);

    $data['page_content'] = 'pages/users_edit';
    $this->load->view('template/template', $data);

    //is the form submitted
    if(form submit){
         if ($this->form_validation->run() == TRUE)
         {
             $this->index();
         }
         else
         {
         $this->load->view('pages/users_edit', $data);
         }
    }
}
$this->load->view('pages/users_edit');

Inside your function update(), after your validation you load view but you don't PASS any data variables to it. 在函数update()中,在验证之后加载视图,但不要将任何数据变量传递给它。 So you don't have any variables which you can access at your view file.. 因此,您没有可以在视图文件中访问的任何变量。

You have to set your variables the same way as in your function edit(): 您必须以与函数edit()中相同的方式设置变量:

$this->load->view('template/template', $data);

Currently there is not set variable $data_user so you can't loop it and use it.. 目前没有设置变量$ data_user,所以你不能循环它并使用它。

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

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