CodeIgniter User Registration Form

In this section, We'll understand how to create simple registration form in CodeIgniter Framework.

To store the customer's account information, a table is required in the database. The following SQL creates a "tbl_customers" table into the MySQL database with some basic required fields.

create a table named "tbl_customers".

CREATE TABLE tbl_customers(
        name VARCHAR(32) NOT NULL,
        email VARCHAR(32) NOT NULL,
	password VARCHAR(32) NOT NULL,
        contact_no VARCHAR(32) NOT NULL,
        primary key(id)

Using a text editor, create a controller called Registration.php. In it, place this code and save it to your application/controllers/ directory:

Callbacks: Your own Validation Methods

The validation system supports callbacks to your own validation methods. This permits you to extend the validation class to meet your needs. For example, if you need to run a database query to see if the customer is choosing a unique email, you can create a callback method that does that and add a new method called check_customer()

Note: You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.

   class Registration extends CI_Controller {
      public function __construct() { 
         $this->load->helper(array('form', 'url')); 
      public function index() {
         /* Load form validation library */ 
	 /* Validation rule */
	 $this->form_validation->set_rules('name', 'Name', 'required');
	 $this->form_validation->set_rules('email', 'Email', 'required|valid_email|callback_check_customer');
	 $this->form_validation->set_rules('password', 'Password', 'required|min_length[6]|max_length[15]');
	 $this->form_validation->set_rules('contact_no', 'Contact Number', 'required');		 
         if ($this->form_validation->run() == FALSE) { 
         else { 
		    $success = "Your account has been successfully created!";
                    $this->load->view('reg_form', compact('success')); 
	  public function check_customer($email)
	         $query = $this->db->where('email', $email)->get("tbl_customers");
		 if ($query->num_rows() > 0)
			 $this->form_validation->set_message('check_customer','The '.$email.' belongs to an existing account');
		        return FALSE;
			  return TRUE;

Using a text editor, create a form called reg_form.php. In it, place this code and save it to your application/views/ directory:

<!DOCTYPE html>
<html lang="en">
<link rel="stylesheet" href="" />
<script src=""</script>
<script src=""></script>
<div class="container">
  <div class="col-md-2"></div>
     <div class="col-md-8" style="margin-top:20px">
		   echo form_open('registration/index');
		   echo validation_errors();
		   if (isset($success))
		   echo '<p>'.$success.'</p>';
		<div class="form-group">
			<label for="email">Name:</label>
			<input type="text" id="name" name="name" value="" />
		<div class="form-group">
			<label for="email">Email:</label>
			<input type="text" id="email" name="email" value="" />
		<div class="form-group">
			<label for="password">Password:</label>
			<input type="password" id="password" name="password" />
		<div class="form-group">
			<label for="contact_no">Contact Number:</label>
			<input type="text" id="contact_no" name="contact_no" value="" />
		<button type="submit" class="btn btn-success">Submit</button>
		echo form_close(); 
	 <div class="col-md-2"></div>

Using a text editor, create a model called Reg_model.php. In it, place this code and save it to your application/models/ directory:

class Reg_model extends CI_Model
    public function saveCustomer()
	   $data['name'] = $this->input->post('name');
	   $data['email'] = $this->input->post('email');
	   $data['password'] = md5($this->input->post('password'));
	   $data['contact_no'] = $this->input->post('contact_no');
	   $this->db->insert('tbl_customers', $data);

note: We are saving the password into database in md5 format.

md5() function calculates the MD5 hash of a string and uses the RSA Data Security, Inc. MD5 Message-Digest Algorithm.

To try your registration form, visit your site using a URL similar to this one:


codeigniter create user registration form codeigniter create user registration form2 codeigniter create user registration form3 codeigniter create user registration form4