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

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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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