Press "Enter" to skip to content

User Registration Form in Codeigniter – Simple

Creating a user registration form is a common task in almost applications. User registration form includes student’s registration, employee registration or any account registration.

In CodeIgniter, every process is going under a MVC concept, that are Model, View and Controller.
So in our user registration form, HTML design for registration form is located at the view section contains textboxes for entering name, address, age, gender, phone, email, etc and a button to submit those data into the database.. it’s up to you to choose the items.
In this example program, we accept Name, Address, Age, Gender, Phone, Email address and password of the user.

As we shown in our tutorial, let’s create HTML design with on CodeIgniter View page.

View (registration.php)

<html>
<head>
<title>Registration</title>
</head>
<body bgcolor="#b1ac4e">
 <center>
 <div class="maindiv">
 <h3>Registration</h3>
 <form action="userregister" method="post">
  <table>
   <tr>
     <td><b>Name </b> </td><td><input type="text" name="username"/></td>
   </tr>
   <tr>
     <td><b>address</b> </td><td> <textarea name="address" style="width:173px;"></textarea></td>
   </tr>
   <tr>
     <td> <b>Age  </b> </td><td><input type="number" name="userage"/></td>
   </tr>
   <tr>
   <tr>
     <td><b>Gender </b> </td><td>
     <select name="gender" style="width:173px;">
    <option value="Male">Male</option>
    <option value="Female">Female</option>
     </select></td>
  </tr>
   <tr>
    <td><b>Phone</b> </td><td> <input type="number" name="userphone"/></td>
  </tr>
   <tr>
    <td> <b>Email  </b> </td><td><input type="email" name="usermail"/></td>
   </tr>
   <tr>
     <td> <b>Password  </b> </td><td><input type="password" name="userpwd"/></td>
          </tr>
   <tr>
    <td> <b> </b> </td><td> <input type="submit" value="Register" style="height: 25px; width: 100px; float: right;"/></td>
   </tr>
 </table>
      </form>
     <div>
   </center>
</body>
</html>

<style>
.maindiv
{
margin-top: 15%;
background: gray;
padding: 15px;
width: 275px;
height: auto;
border: 5px solid black;
}
</style>

Output:

Simple user registration in codeIgniter

Here table tag is used to align elements and contains some CSS codes for design.

Controller (user.php)

Here controller gets all View page posted data and sends them into the Model class.

<?php
class User extends CI_Controller
 {
 public function register()
  {
    $this->load->view("user/registration");
  }
 public function userregister()
   {
      $name = $this->input->post("username");
      $address = $this->input->post("address");
      $age = $this->input->post("userage");
      $gender = $this->input->post("gender");
      $phone = $this->input->post("userphone");
      $email = $this->input->post("usermail");
      $password = $this->input->post("userpwd");

$this->load->Model(‘ProjectModel’);

$this->ProjectModel->USERNAME = $name;
$this->ProjectModel->ADDRESS = $address;
$this->ProjectModel->AGE = $age;
$this->ProjectModel->GENDER = $gender;
$this->ProjectModel->PHONE = $phone;
$this->ProjectModel->EMAIL = $email;
$this->ProjectModel->PASSWORD = $password;

$this->ProjectModel->adduser();
}
}

Where function register() is our first controller function that loads the registration.php

Model (ProjectModel.php)

<?php
class ProjectModel extends CI_Model
{
  var $USERNAME = '';
  var $ADDRESS = '';
  var $AGE = 0;
  var $GENDER = '';
  var $PHONE = 0;
  var $EMAIL = '';
  var $PASSWORD = '';

public function adduser()
{
$this->load->database();
$data = array(
‘user_name’ => $this->USERNAME,
‘user_address’ => $this->ADDRESS,
‘user_age’ => $this->AGE,
‘user_gender’ => $this->GENDER,
‘user_phone’ => $this->PHONE,
‘user_email’ => $this->EMAIL,
‘user_pwd’ => $this->PASSWORD
);
$this->db->insert(‘table_user’, $data);
}
}

 

Database design:

CodeIgniter user register database design

Now you have successfully completed a user registration process. Now let’s create a login page to log a user in to the account using email and password.

2 Comments

  1. sudhin sudhin 7th September 2019

    pls share upload image for registration

    • Aslam Kakkove Aslam Kakkove Post author | 10th September 2019

      Here you can learn now to upload images or any files:
      http://www.yourowncodes.com/2017/11/getting-uploaded-files-in-codeigniter-from-database.html

Leave a Reply