duanba3707 2016-07-27 13:52
浏览 28
已采纳

PHP Codeigniter - 严重性:控制器和视图的注意事项

I'm trying to implement User Registration using PHP codeigniter for same I have created Model, View and controller for it.

I'm facing some minor error i.e

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: errors
Filename: register_user.php

i have define variable errors in users.php then also why it is poping out undefined variable error.

Below is users.php (Controller)

function register(){
        if($_POST){

            $config=array(
                array(
                    'field'=>'username',
                    'label'=>'Username',
                    'rules'=>'trim|required|min_length[3]|is_unique[users.username]'
                ),
                array(
                    'field'=>'password',
                    'label'=>'Password',
                    'rules'=>'trim|required|min_length[5]'
                ),
                 array(
                    'field'=>'password2',
                    'label'=>'Password confirmed',
                    'rules'=>'trim|required|min_length[5]|matches[password]'
                ),
                 array(
                    'field'=>'email',
                    'label'=>'Email',
                    'rules'=>'trim|required|is_unique[users.email]|valid_email'
                )
                );
            $this->load->library('form_validation');
            $this->form_validation->set_rules($config);
            if($this->form_validation->run() == FALSE){
                $data['errors']=validation_errors();
            }else{
                    $data=array(
                    'username'=>$_POST['username'],
                    'password'=>$_POST['password'],
                    'user_type'=>$_POST['user_type'],
                    );
                $this->load->model('user');
                $userid=$this->user->create_user($data);
                $this->session->set_userdata('userID',$userid);
                $this->session->set_userdata('user_type'.$_POST['user_type']);
                redirect(base_url().'posts');
            }
        }
        $this->load->helper('form');
       // $this->load->view('header');
        $this->load->view('register_user');
      //  $this->load->view('footer');
    }


Below is user.php (model)

    function create_user($data){
        $this->db->insert('users',$data);
    }


Below is register_user.php (View)

    <?php if($errors){ ?>
<div style="background:red; color:white;">
    <?php echo $errors ?>
</div>

<?php } ?>

<?php echo form_open(base_url().'users/register'); ?>
    <p>Username: <?php
                 $data_form=array(
                     'name'=>'username',
                     'size'=>50,
                     'style'=>'border:1px solid black',
                     'id'=>'username'
                     );  
                 echo form_input($data_form);                 
                 ?> 
    </p>

<p>
    <?php echo form_label('Email','email')?>: <?php
                                                    $data_form=array(
                                                        'name'=>'email',
                                                        'size'=>50,
                                                        'style'=>'border:1px solid black',
                                                        'id'=>'email'
                                                        );
                                                    echo form_input($data_form);
                                                    ?>
</p>

    <p><?php echo form_label('Password','password')?>: <?php
             $data_form=array(
                 'name'=>'password',
                 'size'=>50,
                 'style'=>'border:1px solid black',
                 'id'=>'password'
                 );  
             echo form_password($data_form);                 
                                                       ?> 
    </p>

<p>
    <?php echo form_label('Password Confirmed','password2')?>: <?php
                                                    $data_form=array(
                                                        'name'=>'password2',
                                                        'size'=>50,
                                                        'style'=>'border:1px solid black',
                                                        'id'=>'password2'
                                                        );
                                                    echo form_password($data_form);
                                                    ?>
</p>

    <p>
        <?php echo form_label('User Type','user_type')?>:<?php
                  $options=array(
                      ''=>'--',
                      'admin'=>'Admin',
                      'author'=>'Author',
                      'user'=>'User',
                      );
                  $js='onchange="alert(\'hi\');" style="border:1px solid black"';
                  echo form_dropdown('user_type',$options,'',$js);
        ?>
    </p>
    <p><?php echo form_submit('','Register'); ?></p>
<?php echo form_close(); ?>

Please me out.

  • 写回答

1条回答 默认 最新

  • douzizang7783 2016-07-27 14:07
    关注

    The error is clearing giving you an hint about what the error is :

    The below error means that the variable does not exists at all and it is not set anywhere.

    Notice Message: Undefined variable: errors Filename: register_user php

    What you are doing is checking for the variable $errors inside the view , but that variable does not exists. And why that variable does not exists ? .

    Because Your are defining the $data['errors'] , but you have never passed it to the view: look at your line :

     $this->load->view('register_user');
    

    it needs to be like below:

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

    and inside even if the variable does not exists , you can check also if variable is set or not by using isset() inside the view , so it needs to be like this inside register_user.php :

    <?php if(isset($errors)){ ?>
    <div style="background:red; color:white;">
        <?php echo $errors; ?>
    </div>
    
    <?php } ?>
    

    Also one more thing. Inside codeigniter consider using codeigniter's input class :

    $this->input->post('username'); // for $_POST 
    $this->input->get('username'); // for $_GET
    $this->input->cookie('cookie_name');
    $this->input->server();
    

    instead of using $_POST , $_GET

    As the codeigniter guide states :

    The Input Class serves two purposes:

    1. It pre-processes global input data for security.
    2. It provides some helper methods for fetching input data and pre-processing it.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料