Categories Uncategorised

Make Registration form in CodeIgniter

1] Download the fresh CodeIgniter Project.
2] Make the database in phpmyadmin like users
Execute these commond on SQl
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;
3] Open file database from Project >> config >>database.php and change username, database name etc.

Like as

$db[‘default’] = array(
‘dsn’ => ”,
‘hostname’ => ‘localhost’,
‘username’ => ‘root’,
‘password’ => ”,
‘database’ => ‘CodeIgniter-3.1.8’,
‘dbdriver’ => ‘mysqli’,
‘dbprefix’ => ”,
‘pconnect’ => FALSE,
‘db_debug’ => (ENVIRONMENT !== ‘production’),
‘cache_on’ => FALSE,
‘cachedir’ => ”,
‘char_set’ => ‘utf8’,
‘dbcollat’ => ‘utf8_general_ci’,
‘swap_pre’ => ”,
‘encrypt’ => FALSE,
‘compress’ => FALSE,
‘stricton’ => FALSE,
‘failover’ => array(),
‘save_queries’ => TRUE
);

4] Open the autoload.php file from Project >> config >autoload.php then change two things like as

$autoload[‘libraries’] = array(‘database’);
$autoload[‘helper’] = array(‘form’);

5] Now create controller file , model file and view file like as

view file “register.php”
contorller file “Users.php”
model file “Users_model.php”

register.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div class=”row page-content”>
<div class=”col-lg-12″>
<h2>Register Form</h2>
<?php if(validation_errors()) { ?>
<div class=”alert alert-danger”>
<?php echo validation_errors(); ?>
</div>
<?php } ?>
<?php echo form_open(‘users/actionregister’); ?>

<div class=”form-group”>
<input type=”text” name=”name” class=”form-control” id=”name” placeholder=”Name”>
</div>
<div class=”form-group”>
<input type=”text” name=”username” class=”form-control” id=”username” placeholder=”User Name”>
</div>
<div class=”form-group”>
<input type=”text” name=”email” class=”form-control” id=”email” placeholder=”Email”>
</div>
<div class=”form-group”>
<input type=”password” name=”password” class=”form-control” id=”password” placeholder=”Password”>
</div>
<div class=”form-group”>
<input type=”password” name=”confirm_password” class=”form-control” id=”confirm-password” placeholder=”Confirm Password”>
</div>
<div class=”form-group pull-right”>
<button type=”submit” id=”register” class=”btn btn-primary”>Register</button>
</div>
</div>
<?php echo form_close(); ?>

</div>
</body>
</html>

 

Users.php

<?php
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(‘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’);
$data[‘title’] = ‘Register – Tech Arise’;
$data[‘metaDescription’] = ‘Register’;
$data[‘metaKeywords’] = ‘Register’;
$this->load->view(‘register’, $data);
}
}
}
?>

 

 

Users_model.php

<?php

/*
* 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(‘users’, $data);
return $this->db->insert_id();
}
}
?>

After than run the Project as

http://localhost/projectsname/

Leave a Reply

Your email address will not be published. Required fields are marked *