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(
id INT AUTO_INCREMENT,
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.
<?php
class Registration extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
/* Load form validation library */
$this->load->library('form_validation');
/* 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) {
$this->load->view('reg_form');
}
else {
$this->load->model('reg_model');
$this->reg_model->saveCustomer();
$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;
}
else
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">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="col-md-2"></div>
<div class="col-md-8" style="margin-top:20px">
<?php
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>
<div class="form-group">
<label for="email">Email:</label>
<input type="text" id="email" name="email" value="" />
</div>
<div class="form-group">
<label for="password">Password:</label>
<input type="password" id="password" name="password" />
</div>
<div class="form-group">
<label for="contact_no">Contact Number:</label>
<input type="text" id="contact_no" name="contact_no" value="" />
</div>
<button type="submit" class="btn btn-success">Submit</button>
<?php
echo form_close();
?>
</div>
<div class="col-md-2"></div>
</body>
</html>
Using a text editor, create a model called Reg_model.php. In it, place this code and save it to your application/models/
directory:
<?php
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:
http://localhost/codeigniter/index.php/registration/index