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.