In this post, We have share How to Create Registration Form in CodeIgniter and Bootstrap Framework along with MySQL Database.By using this user can easily register, username, first name, last name, email, password fields. After submitting form, the data will be stored in a database table.
Step 1: Create MySQL Database and Table for User Registration
Step 2: Create a model file User Registration
Create a model file named "Users_model.php" inside "application/models" folder.
Step 3: Create a controller file User Registration
Next create a controller file named "Users.php" inside "application/controllers" folder.
Step 4: Create a view file User Registration
Create a view file named "register.php" inside "application/views/users" folder
So open "application/config/routes.php" file and add code like as bellow:
Demo [sociallocker] Download[/sociallocker]
Step 1: Create MySQL Database and Table for User Registration
// Table structure for table `users`
CREATE TABLE `users` (
`user_id` int(12) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`user_name` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL,
`password` varchar(255) NOT NULL,
`status` int(1) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
// Indexes for table `users`
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `email` (`email`);
// AUTO_INCREMENT for table `users`
ALTER TABLE `users`
MODIFY `user_id` int(12) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
?>
Step 2: Create a model file User Registration
Create a model file named "Users_model.php" inside "application/models" folder.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Export Controller
*
* @author TechArise Team
*
* @email info@techarise.com
*/
defined('BASEPATH') OR exit('No direct script access allowed');
class Users_model extends CI_Model {
// declare private variable
private $_userID;
private $_name;
private $_userName;
private $_email;
private $_password;
private $_status;
public function setUserID($userID) {
$this->_userID = $userID;
}
public function setName($name) {
$this->_name = $name;
}
public function setUserName($userName) {
$this->_userName = $userName;
}
public function setEmail($email) {
$this->_email = $email;
}
public function setPassword($password) {
$this->_password = $password;
}
public function setStatus($status) {
$this->_status = $status;
}
public function createUser() {
$data = array(
'name' => $this->_name,
'email' => $this->_email,
'user_name' => $this->_userName,
'password' => $this->_password,
'status' => $this->_status,
);
$this->db->insert('user', $data);
return $this->db->insert_id();
}
}
?>
Step 3: Create a controller file User Registration
Next create a controller file named "Users.php" inside "application/controllers" folder.
defined('BASEPATH') OR exit('No direct script access allowed');
class Users extends CI_Controller {
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of Export Controller
*
* @author TechArise Team
*
* @email info@techarise.com
*/
public function __construct() {
parent::__construct();
$this->load->model('Users_model', 'user');
}
// Dashboard
public function index()
{
$data['title'] = 'Dashboard - Tech Arise';
$data['metaDescription'] = 'Dashboard';
$data['metaKeywords'] = 'Dashboard';
$this->load->view('users/dashboard', $data);
}
// Register
public function register()
{
$data['title'] = 'Register - Tech Arise';
$data['metaDescription'] = 'Register';
$data['metaKeywords'] = 'Register';
$this->load->view('users/register', $data);
}
// Action Register
public function actionRegister()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'User Name', 'trim|required|min_length[4]');
$this->form_validation->set_rules('email', 'Your Email', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('confirm_password', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE) {
$this->register();
} else {
// post values
$name = $this->input->post('name');
$username = $this->input->post('username');
$email = $this->input->post('email');
$password = $this->input->post('password');
// set post values
$this->user->setName($name);
$this->user->setUserName($username);
$this->user->setEmail($email);
$this->user->setPassword(MD5($password));
$this->user->setStatus(1);
// insert values in database
$this->user->createUser();
redirect('users/index');
}
}
}
?>
Step 4: Create a view file User Registration
Create a view file named "register.php" inside "application/views/users" folder
Register Form
So open "application/config/routes.php" file and add code like as bellow:
// routes
$route['default_controller'] = 'users/register';
?>
Demo [sociallocker] Download[/sociallocker]