Codeigniter is one of the most popular PHP MVC framework available right now. It comes with bunch of built-in libraries and helper functions like database, form validation, session, form helper, database helper, shopping cart library. Codeigniter is not like another PHP framework in which you have to adjust some system configurations or install some dependencies. You just need to download codeigniter set base path, enter database credentials and you are good to go.
So in this tutorial I am going to show you how to create registration form in codeigniter. This tutorial is for the beginner so I am not going to cover email verification after registration. I will just cover registration form in very simple manner. I will cover email verification in my coming tutorials.
In this tutorial I am not going to cover codeigniter installation. If you are new to codeigniter then please read How to install Codeigniter in Xampp first or if your not familiar with codeigniter directory structure then visit Codeigniter Framework Directory Structure.
Steps involved in creating registration form in Codeigniter
- Create registration form
- Submit registration form and validate form fields.
- Check if user is new or already exists.
- If user is new insert user information into database and redirect user to thankyou page.
- If for any reason user information will not savethen through an error.
Create Database:
1 2 3 4 |
Create database demo; Use demo; |
Create database Table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
CREATE TABLE `users` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `first_name` VARCHAR(255) NULL DEFAULT NULL, `last_name` VARCHAR(255) NULL DEFAULT NULL, `email` VARCHAR(255) NULL DEFAULT NULL, `phone` VARCHAR(255) NOT NULL, `password` VARCHAR(255) NULL DEFAULT NULL, `created` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) COLLATE='latin1_swedish_ci' ENGINE=InnoDB AUTO_INCREMENT=11; |
Codeigniter Setting:
- Setup base path in
config/config.php
- Add database credentials in
config/database.php
- Autoload database library in
config/autoload.php
like$autoload['libraries'] = array('database');
- Autoload url helper in
config/autoload.php
like$autoload['helper'] = array('url');
- Add user as default controller in
config/routes.php
Create Controller:
Users.php
Users.php is a controller file in controllers folder that has 4 functions which are:
- __construct() : calls parent constructer
- Index(): load register form
- RegisterUser(): save register form data
- Thankyou(): display thankyou page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User extends CI_Controller { function __construct() { parent::__construct(); $this->load->library('form_validation'); $this->load->model('User_model'); } public function index() { $this->load->view('register'); } public function RegisterUser() { $this->form_validation->set_rules('first_name', 'First Name', 'trim|required'); $this->form_validation->set_rules('last_name', 'Last Name', 'trim|required'); $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email'); $this->form_validation->set_rules('confirm_email', 'Confirm Email', 'trim|required|matches[email]'); $this->form_validation->set_rules('password', 'Password', 'trim|required'); $this->form_validation->set_rules('phone', 'Phone', 'trim|required'); $this->form_validation->set_error_delimiters('<div class="error-msg">', '</div>'); if ($this->form_validation->run() == FALSE) { $this->load->view('register'); } else { $firstName = $this->security->xss_clean($this->input->post('first_name')); $lastName = $this->security->xss_clean($this->input->post('last_name')); $email = $this->security->xss_clean($this->input->post('email')); $password = $this->security->xss_clean($this->input->post('password')); $phone = $this->security->xss_clean($this->input->post('phone')); $options = array("cost"=>4); $hashPassword = password_hash($password,PASSWORD_BCRYPT,$options); $insertData = array('first_name'=>$firstName, 'last_name'=>$lastName, 'email'=>$email, 'phone'=>$phone, 'password'=>$hashPassword); $checkDuplicate = $this->User_model->checkDuplicate($email); if($checkDuplicate == 0) { $insertUser = $this->User_model->insertUser($insertData); if($insertUser) { redirect('user/thankyou'); } else { $data['errorMsg'] = 'Unable to save user. Please try again'; $this->load->view('register',$data); } } else { $data['errorMsg'] = 'User email alreary exists'; $this->load->view('register',$data); } } } function thankyou() { $this->load->view('thankyou'); } } |
Create Model:
User_model.php
User_model.php is model file in models folder that has 3 functions which are:
__construct()
: calls parent construct.checkDuplicate()
: check email is new or already in the table.insertUser()
: insert user information into the table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); class User_model extends CI_Model { function __construct() { parent::__construct(); } function checkDuplicate($email) { $this->db->select('email'); $this->db->from('users'); $this->db->like('email', $email); return $this->db->count_all_results(); } function insertUser($data) { if($this->db->insert('users', $data)) { return $this->db->insert_id(); } else { return false; } } } |
Create Views:
Register.php
- Using bootstrap css and jquery link.
validation_errors()
is used to print form validation errors.$errorMsg
is used to print duplicate email and unable to save errors- Form will be post on
RegisterUser
function set_value()
function is used to print post value in the input field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
<!doctype html> <html> <head> <title>Registration form using codeigniter 3</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> </head> <style> body{ background:#d9edf7; } .custom-bottom-margin{ padding-bottom:30px; } .error-msg{ margin:5px auto; width:30%; background:#db3737; color:#ffffff; } </style> <body > <div class="container"> <div class="row"> <div class="col-md-12 text-center"> <h2>Registration Form</h2> </div> </div> <div class="row"> <?php echo validation_errors(); if(isset($errorMsg)) { echo '<div class="error-msg">'; echo $errorMsg; echo '</div>'; unset($errorMsg); } ?> <form action="<?php echo base_url()."index.php/user/RegisterUser"?>" method="post"> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="name">First Name</label> <div class="col-sm-5"> <input type="text" name="first_name" class="form-control" value="<?php echo set_value('first_name'); ?>" placeholder="Enter First name" id="fname"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="name">Last Name</label> <div class="col-sm-5"> <input type="text" name="last_name" class="form-control" value="<?php echo set_value('last_name'); ?>" placeholder="Enter Last name" id="lname"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="email">Email</label> <div class="col-sm-5"> <input type="email" name="email" class="form-control" value="<?php echo set_value('email');?>" placeholder="Enter email" id="email"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="confirm_email">Confirm Email</label> <div class="col-sm-5"> <input type="text" name="confirm_email" class="form-control" value="<?php echo set_value('confirm_email');?>" placeholder="Confirm email" id="confirm_email"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="password">Password</label> <div class="col-sm-5"> <input type="password" name="password" class="form-control" value="<?php echo set_value('password');?>" placeholder="Enter password" id="password"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right" for="phone">Phone</label> <div class="col-sm-5"> <input type="text" name="phone" class="form-control" value="<?php echo set_value('phone');?>" placeholder="Enter phone" id="phone"> </div> </div> <div class="form-group custom-bottom-margin"> <label class="control-label col-sm-4 text-right"></label> <div class="col-sm-5"> <button class="btn btn-primary" type="submit"> Submit</button> </div> </div> </form> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html> |
Thankyou.php
Just print thank you message after insertion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<!doctype html> <html> <head> <title>Thank you Registration</title> <!-- Latest compiled and minified CSS --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <!-- Optional theme --> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> </head> <style> body{ background:#d9edf7; } .custom-bottom-margin{ padding-bottom:30px; } </style> <body > <div class="container"> <div class="row"> <div class="col-md-12 text-center"> <h2>Thank You</h2> </div> </div> <div class="row"> <div class="col-md-12 text-center"> Thank you for you regisration </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> </body> </html> |