Harshad Patil Harshad Patil - 4 months ago 4
PHP Question

PHP Codeigniter - Severity: Notice for Controller and view

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.

Answer

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.
Comments