CodeIgniter Register with Validation

Installing CodeIgniter

If you don't have CodeIgniter installed yet, you can use this link to download the latest version of CodeIgniter which is 3.1.7 that I've used in this source code. After downloading, extract the file in the folder of your server. Since I'm using XAMPP as my localhost server, I've put the folder in htdocs folder of my XAMPP. Then, you can test whether you have successfully installed codeigniter by typing your app name in your browser. In my case, I named my app as codeigniter_register so I'm using the below code.
  1. localhost/codeigniter_register/
codeigniter successful install

Creating our Database

First we are going to create our database for this source code. 1. Open your phpMyAdmin. 2. Create a new database named codeigniter. 3. Click the database that we created, click SQL tab then paste the below code or import the included .sql file in the downloadable of this source code located in db folder.
  1. CREATE TABLE `users` (
  2. `email` varchar(40) NOT NULL,
  3. `password` varchar(40) NOT NULL,
  4. `fname` varchar(100) NOT NULL,
database mysql

Connecting our App to Database

Next, we're going to connect our codeigniter application to the database that we created earlier. 1. In your codeigniter app folder, open database.php located in application/config folder. 2. Update database.php with your credential the same as what I did in the image below. database is where we define the database that we created earlier. codeigniter database config

Configuring Base URL

Next, we configure our base url to tell codeigniter that this is the URL of our site/application. We're gonna be using this a lot that's why we need to configure this. 1. In your codeigniter app folder, open config.php located in application/config folder. 2. Find and edit the ff line:
  1. $config['base_url'] = 'http://localhost/codeigniter_register';
where codeigniter_register is the name of your app folder.

Creating our Model

Next, we create the model for our app. Take note that the first letter of your model name should be in CAPITAL letter and the name of the model should be the same as the file name to avoid confusion. Create a file named Users_model.php in application/models folder of our app and put the ff codes.
  1. <?php
  2. class Users_model extends CI_Model {
  3. function __construct(){
  4. parent::__construct();
  5. $this->load->database();
  6. }
  7.  
  8. public function getAllUsers(){
  9. $query = $this->db->get('users');
  10. return $query->result();
  11. }
  12.  
  13. public function register($user){
  14. return $this->db->insert('users', $user);
  15. }
  16.  
  17. }
  18. ?>

Creating our Controller

Next step is to create our controller. Controllers follow the same naming convention as models. Create a file named User.php in application/controllers folder of our app and put the ff codes.
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class User extends CI_Controller {
  5.  
  6. function __construct(){
  7. parent::__construct();
  8. $this->load->model('users_model');
  9. // load form and url helpers
  10. $this->load->helper(array('form', 'url'));
  11. // load form_validation library
  12. $this->load->library('form_validation');
  13. }
  14.  
  15. public function index(){
  16. //load session library
  17. $this->load->library('session');
  18. $data['users'] = $this->users_model->getAllUsers();
  19. $this->load->view('register_form', $data);
  20. }
  21.  
  22. public function register(){
  23. //load session library
  24. $this->load->library('session');
  25. /* Set validation rule for name field in the form */
  26. $this->form_validation->set_rules('email', 'Email', 'valid_email|required');
  27. $this->form_validation->set_rules('password', 'Password', 'required|min_length[7]|max_length[30]');
  28. $this->form_validation->set_rules('fname', 'Full Name', 'required');
  29.  
  30. if ($this->form_validation->run() == FALSE) {
  31. $data['users'] = $this->users_model->getAllUsers();
  32. $this->load->view('register_form', $data);
  33. }
  34. else {
  35. $user['email'] = $_POST['email'];
  36. $user['password'] = $_POST['password'];
  37. $user['fname'] = $_POST['fname'];
  38.  
  39. $query = $this->users_model->register($user);
  40.  
  41. if($query){
  42. header('location:'.base_url().$this->index());
  43. $this->session->set_flashdata('success','User registered successfully');
  44. }
  45. else{
  46. header('location:'.base_url().$this->index());
  47. $this->session->set_flashdata('error','Failed to register user');
  48. }
  49. }
  50. }
  51.  
  52. }

Creating our Default Route

Next, we are going to set our default route so that whenever we haven't set up a controller to use, this default controller will be used instead. Open routes.php located in application/config folder and set the default route to our user controller. Note: While we name controllers using CAPITAL letter in this first letter, we refer to them in SMALL letter.
  1. $route['default_controller'] = 'user';

Creating our View

Lastly, we create the views of our app. Take note that I've use Bootstrap in the views. You may download bootstrap using this link. Create the ff files inside application/views folder. register_form.php This contains our registration form and I've also included our users table to show that the registered user is inserted into the database.
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeIgniter Register</title>
  6. <link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>bootstrap/css/bootstrap.min.css">
  7. </head>
  8. <body>
  9. <div class="container">
  10. <h1 class="page-header text-center">CodeIgniter Register with Validation</h1>
  11. <div class="row">
  12. <div class="col-sm-4">
  13. <div class="login-panel panel panel-primary">
  14. <div class="panel-heading">
  15. <h3 class="panel-title"><span class="glyphicon glyphicon-user"></span> Register
  16. </h3>
  17. </div>
  18. <div class="panel-body">
  19. <form method="POST" action="<?php echo base_url(); ?>index.php/user/register">
  20. <fieldset>
  21. <div class="form-group">
  22. <input class="form-control" placeholder="Email" type="text" value="<?php echo set_value('email'); ?>" name="email">
  23. </div>
  24. <div class="form-group">
  25. <input class="form-control" placeholder="Password" type="password" value="<?php echo set_value('password'); ?>" name="password">
  26. </div>
  27. <div class="form-group">
  28. <input class="form-control" placeholder="Full Name" type="text" value="<?php echo set_value('fname'); ?>" name="fname">
  29. </div>
  30. <button type="submit" class="btn btn-lg btn-primary btn-block"><span class="glyphicon glyphicon-check"></span> Sign Up</button>
  31. </fieldset>
  32. </form>
  33. </div>
  34. </div>
  35. <?php
  36. if(validation_errors()){
  37. ?>
  38. <div class="alert alert-danger text-center" style="margin-top:20px;">
  39. <?php echo validation_errors(); ?>
  40. </div>
  41. <?php
  42. }
  43.  
  44. if($this->session->flashdata('error')){
  45. ?>
  46. <div class="alert alert-danger text-center" style="margin-top:20px;">
  47. <?php echo $this->session->flashdata('error'); ?>
  48. </div>
  49. <?php
  50. }
  51.  
  52. if($this->session->flashdata('success')){
  53. ?>
  54. <div class="alert alert-success text-center" style="margin-top:20px;">
  55. <?php echo $this->session->flashdata('success'); ?>
  56. </div>
  57. <?php
  58. }
  59. ?>
  60. </div>
  61. <div class="col-sm-8">
  62. <table class="table table-bordered table-striped">
  63. <thead>
  64. <tr>
  65. <th>ID</th>
  66. <th>FullName</th>
  67. <th>Email</th>
  68. <th>Password</th>
  69. </tr>
  70. </thead>
  71. <tbody>
  72. <?php
  73. foreach($users as $user){
  74. ?>
  75. <tr>
  76. <td><?php echo $user->id; ?></td>
  77. <td><?php echo $user->fname; ?></td>
  78. <td><?php echo $user->email; ?></td>
  79. <td><?php echo $user->password; ?></td>
  80. </tr>
  81. <?php
  82. }
  83. ?>
  84. </tbody>
  85. </table>
  86. </div>
  87. </div>
  88. </div>
  89. </body>
  90. </html>
For more list of validation rules, you may visit this link. That ends the explanation for this tutorial. Happy Coding :)

Add new comment