Reputation: 91
register.php - controller
<?php
class Register extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->view('header');
$this->load->view('user/register');
$this->load->view('home');
$this->load->view('footer');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('user/register');
}
else {
header( 'Location: dashboard/lfg' ) ;
}
}
}
?>
register.php - view
<div id="registration" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2>Create Account</h2>
<?php echo validation_errors(); ?>
<?php echo form_open('Register'); ?>
<label>Username</label>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" required />
<label>Email</label>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" required />
<label>Password</label>
<input type="password" name="password" value="<?php echo set_value('password'); ?>" size="50" required />
<label>Confirm Password</label>
<input type="password" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" required />
<div>
<input type="submit" value="Create Account" /></div>
</form>
<a class="modal-link" href="#login">
<p>Already have an account?</p>
</a>
</div>
registration.php - model
<?php
class Registration extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){
// Insert data
$this->db->insert('users', $data);
}
}
?>
The view and controller work as is at the moment. The form "submits" if information entered is correct, ie. passwords match, email is valid, etc, but I have not coded the php to get the data sent to database correctly. I can do this with stmt correctly but I want to use the framework to do everything to do with the database because it's built in.
Does anyone have insight to what I need to do or could give me example code that will allow this to work and get the data sent to the database.
I have checked the codeigniter documentation and found out how to set the validation rules etc but it doesn't seem to go over how to actually send the data to the database.
If anyone has the link to that it would be helpful as well.
Upvotes: 0
Views: 86
Reputation: 110
register.php - controller
<?php
class Register extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->model('Registration');
}
public function index()
{
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->view('header');
$this->load->view('user/register');
$this->load->view('home');
$this->load->view('footer');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('user/register');
}
else {
$data= array("username"=>$this->input->post('username'),
"password"=>$this->input->post('password'));
$this->Registration->form_insert($data);
header( 'Location: dashboard/lfg' ) ;
}
}
}
?>
Upvotes: 0
Reputation: 650
You can also load helper,library and model in --construct() function only one time
<?php
class Register extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('form_validation');
$this->load->model('registration');
}
public function index()
{
$this->form_validation->set_rules('username', 'Username','trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('user/register');
$this->load->view('home');
$this->load->view('footer');
}else {
$data = $this->input->post();
$allowed = array('username','email','password');
$data = array_intersect_key($data,array_flip($allowed));
//$data['password'] = do_hash($data['password']); ...
$this->registration->form_insert($data);
// set sessions and login data ...
redirect('dashboard/lfg');
}
}
Upvotes: 1
Reputation: 2759
You must load the view files at the end of your controller
like this :
<?php
class Register extends CI_Controller {
public function index()
{
$this->load->helper('url');
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[8]',
array('required' => 'You must provide a %s.')
);
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'trim|required|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email');
if ($this->form_validation->run() == FALSE) {
$this->load->view('header');
$this->load->view('user/register');
$this->load->view('home');
$this->load->view('footer');
}else {
$data = $this->input->post();
$allowed = array('username','email','password');
$data = array_intersect_key($data,array_flip($allowed));
//$data['password'] = do_hash($data['password']); ...
$this->load->model('registration');
$this->registration->form_insert($data);
// set sessions and login data ...
redirect('dashboard/lfg');
}
}
}
Upvotes: 0