hi i am currently learning code igniter i am pretty much new to this i have an issue with code igniter validations. I cant figure out what i am doing wrong please guide me.
currently what is happening is that whenever i submit with empty fields i am redirected to same page but when i populate the fields i am redirected to same page but the page is blank.
this is my user class
class User extends CI_Controller {
function User() {
parent::__construct();
$this->view_data['base_url'] = base_url();
}
function index() {
$this->register();
}
function register() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_numeric|min_length[6]');
if ($this->form_validation->run() == false) {
$this->load->view('view_register', $this->view_data);
} else {
$username=$this->input->post('username');
}
}
}
and this is my view_register.php (view)
<body>
<h1>User Registration</h1>
please fill in following details
<?php
echo form_open(base_url().'user/register');
?>
<?php
$username = array(
'name' => 'username',
'id' => 'username',
'value' => ''
);
$password = array(
'name' => 'password',
'id' => 'password',
'value' => ''
);
$con_password = array(
'name' => 'con_password',
'id' => 'con_password',
'value' => ''
);
$email = array(
'name' => 'email',
'id' => 'email',
'value' => ''
);
?>
<ul>
<li>
<label>Username</label>
<div>
<?php echo form_input($username); ?>
</div>
</li>
</ul>
<ul>
<li>
<label>Email</label>
<div>
<?php echo form_input($email); ?>
</div>
</li>
</ul>
<ul>
<li>
<label>Password</label>
<div>
<?php echo form_password($password); ?>
</div>
</li>
</ul>
<ul>
<li>
<label>Confirm</label>
<div>
<?php echo form_password($con_password); ?>
</div>
</li>
</ul>
<ul>
<li>
<div>
<?php echo form_submit(array('name' => 'register'), 'Register'); ?>
</div>
</li>
</ul>
<?php echo form_close(); ?>
</body>
Beacuse you’re not loading a view in case validation is right, so CI calls again that controller, but displays nothing:
Also, you should be opening the form with this, instead of your code:
it works out by itself the correct url (anologuely to site_url());
Try putting, also,
echo form_error('username')under the username input to display the validation errors.