Contact US


Get Your Project Done by our experts
info@fundaofwebit.com

How to create user registration form in codeigniter 3

Whatsapp

https://fundaofwebit.com/post/how-to-create-user-registration-form-in-codeigniter-3

How to create user registration form in codeigniter 3


In this article, we are going to create / make a USER Registration form in codeIgniter 3. we will be seeing following things:

  1. Form validation
  2. Setting success message using session
  3. User Email already exists or not in users table.

Before getting started:

  • Make your database connection ready:
  • Create a table in your database named users as follows:
CREATE TABLE users (
    id INT(11) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    first_name VARCHAR(30) NOT NULL,
    last_name VARCHAR(30) NOT NULL,
    email VARCHAR(50),
    password VARCHAR(50),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)


So Let's get started:

Step 1: Create a URL named register in your routes.php file path:  application/config/routes.php as follows:

$route['register']['GET'] = 'Auth/RegisterController/index';
$route['register']['POST'] = 'Auth/RegisterController/register';


Step 2: Create a register.php file in path: application/views/auth/register.php for form design as follows:

<div class="py-5">
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-7">
                <?php if($this->session->flashdata('status')) : ?>
                    <div class="alert alert-success">
                        <?= $this->session->flashdata('status'); ?>
                    </div>
                <?php endif?>

                <div class="card shadow">
                    <div class="card-header">
                        <h5>Registe</h5>
                    </div>
                    <div class="card-body">
                        <form action="<?php echo base_url('register'?>" method="POST">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">First Name</label>
                                        <input type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" class="form-control">
                                        <small><?php echo form_error('first_name'); ?></small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">Last Name</label>
                                        <input type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" class="form-control">
                                        <small><?php echo form_error('last_name'); ?></small>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="">Email Address</label>
                                        <input type="email" name="email"  value="<?php echo set_value('email'); ?>"  class="form-control">
                                        <small><?php echo form_error('email'); ?></small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">Password</label>
                                        <input type="password" name="password" class="form-control">
                                        <small><?php echo form_error('password'); ?></small>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="">Confirm Password</label>
                                        <input type="password" name="cpassword" class="form-control">
                                        <small><?php echo form_error('cpassword'); ?></small>
                                    </div>
                                </div>
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary px-5">Register Now</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


Step 3: Create a file named header.php file in path: application/views/template/header.php for extending this into your register.php file.

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <title>Funda of Web IT</title>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css">
  </head>
<body>


Step 4: Create a file named footer.php file in path: application/views/template/footer.php for extending this into your register.php file.

    <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js"></script>
                    
</body>
</html>


Step 5: Go to your autoload.php file where path: application/config/autoload.php and add or replace with the below code:

$autoload['libraries'] = array('database','session');


Step 6: Let's Create a controller named RegisterController.php in the path: application/controllers/Auth/RegisterController.php and follow as below:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class RegisterController extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('form');
        $this->load->library('form_validation');

        $this->load->model('UserModel');
    }

    public function index()
    {
        $this->load->view('template/header.php');
        $this->load->view('auth/register.php');
        $this->load->view('template/footer.php');
    }

    public function register()
    {
        $this->form_validation->set_rules('first_name''First Name''trim|required|alpha');
        $this->form_validation->set_rules('last_name''Last Name''trim|required|alpha');
        $this->form_validation->set_rules('email''Email ID''trim|required|valid_email|is_unique[users.email]');
        $this->form_validation->set_rules('password''Password''trim|required');
        $this->form_validation->set_rules('cpassword''Confirm Password''trim|required|matches[password]');
        if($this->form_validation->run() == FALSE)
        {
            // failed
            $this->index();
        }
        else
        {
            $data = array(
                'first_name' => $this->input->post('first_name'),
                'last_name' => $this->input->post('last_name'),
                'email' => $this->input->post('email'),
                'password' => $this->input->post('password')
            );
            $register_user = new UserModel;
            $checking = $register_user->registerUser($data);
            if($checking)
            {
                $this->session->set_flashdata('status','Registered Successfully.! Go to Login');
                redirect(base_url('register'));
            }
            else
            {
                $this->session->set_flashdata('status','Something Went Wrong.!');
                redirect(base_url('register'));
            }
        }
    }
}

?>


Step 7: Create a model named UserModel.php file in path: application/models/UserModel.php as follows:

<?php

class UserModel extends CI_Model
{
    public function registerUser($data)
    {
        return $this->db->insert('users'$data);
    }
}

?>


Thank you for reading..

© Copyright - 2018 - 2021 | All rights reserved at Funda of Web IT