codeigniter complete registration with Callback Function - Web Development and Web Design Codes

Latest

Sunday, February 11, 2018

codeigniter complete registration with Callback Function

Codeigniter Registration System with Callback Function

codeigniter complete registration with Callback Function
codeigniter complete registration with Callback Function

In this lesson we are going to learn Complete Registration System in Codeigniter.
we use Database to Check username Availability and Email Availability  in Codeigniter Callback Function..
Just Follow The Below Codeigniter Registration System Example:
 Database and Table:
Database Name: codenair
Table Name: user
Create Table User:


CREATE TABLE IF NOT EXISTS `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL,
  `passconf` varchar(50) NOT NULL,
  `password` varchar(20) NOT NULL,
  `email` varchar(50) NOT NULL,
  `fname` varchar(20) NOT NULL,
  `lname` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

Configuration:
 application/config/autoload.php
$autoload['helper'] = array('url');

 application/config/config.php
$config['base_url'] = 'http://localhost/registration';

 application/config/database.php
$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
	'dsn'	=> '',
	'hostname' => 'localhost',
	'username' => 'root',
	'password' => '',
	'database' => 'codenair',
	'dbdriver' => 'mysqli',
	'dbprefix' => '',
	'pconnect' => FALSE,
	'db_debug' => (ENVIRONMENT !== 'production'),
	'cache_on' => FALSE,
	'cachedir' => '',
	'char_set' => 'utf8',
	'dbcollat' => 'utf8_general_ci',
	'swap_pre' => '',
	'encrypt' => FALSE,
	'compress' => FALSE,
	'stricton' => FALSE,
	'failover' => array(),
	'save_queries' => TRUE
);
Controllers: blog.php
application/controllers/blog.php
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Blog extends CI_Controller {
	public function __construct(){
		parent::__construct();
		$this->load->model('Reg_model');
		$this->load->helper('url','email');
		$this->load->library('form_validation');
		$this->load->helper("security");
	}
	public function index()
	{
		$this->load->view('index');
	}
	public function registration(){
		//Validating Form
	$this->form_validation->set_rules('username','UserName','trim|required|min_length[6]|max_length[12]|callback_checkuser|xss_clean');
		$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
		$this->form_validation->set_rules('passconf','Confirmation Password','trim|required|matches[password]|xss_clean');
	$this->form_validation->set_rules('email','Email','trim|required|valid_email|callback_checkemail|xss_clean');
		$this->form_validation->set_rules('fname','First Name','trim|required|xss_clean');
		$this->form_validation->set_rules('lname','First Name','trim|required|xss_clean');
		
		if($this->form_validation->run() == FALSE){
			$this->load->view('index');
		}else{
		$data = array(
        'username' => $this->input->post('username'),
        'password' => $this->input->post('password'),
		'passconf' => $this->input->post('passconf'),
        'email' => $this->input->post('email'),
		'fname' => $this->input->post('fname'),
		'lname' => $this->input->post('lname')
		);
		//Loading Model
		 $data['result']=$this->Reg_model->regsuccess($data);
		 //Send Success Message
		 $data['message']='Registration Successfully Completed';
		 $this->load->view('index',$data);
			  
		}
	}
	//Checking Existing UserName
	public function checkuser(){
		$username=$this->input->post('username');
		$result=$this->Reg_model->usercheck($username);
		if(count($result)==0){
			return TRUE;
		}else{
			$this->form_validation->set_message('checkuser','<font color=green>Your {field} :<font color=red>'.$username.'</font> Already Taken</font>');
			return FALSE;
		}
	}
	//Checking Existing Email
	public function checkemail(){
		$email=$this->input->post('email');
		$result=$this->Reg_model->emailcheck($email);
		if(count($result)==0){
			return TRUE;
		}else{
			$this->form_validation->set_message('checkemail','<font color=green>Your {field} :<font color=red>'.$email.'</font> Already Taken</font>');
			return FALSE;
		}
	}
}
?>
Models: reg_model.php
application/models/reg_model.php
<?php
class Reg_model extends CI_model{
	public function __construct(){
		parent::__construct();
		$this->load->database();
	}
   public function regsuccess($data){
	   $query=$this->db->insert('user', $data);
   }
   //Checking Existing UserName
   public function usercheck($username){
	   $query=$this->db->query("SELECT*FROM user WHERE username='$username'");
	   return $query->result_array();
   }
   //Checking Existing Email
   public function emailcheck($email){
	   $query=$this->db->query("SELECT*FROM user WHERE email='$email'");
	   return $query->result_array();
   }
}
?>
Views : index.php

application/views/index.php
<style>
  .error{
	  color:red;
	  font-weight:bold;
	  font-style:italic;
  }
  input[type=text]{
	  padding:5px;
	  border-radius:4px;
  }
  input[type=submit]{
	  padding:7px;
	  background:black;
	  color:white;
	  border-radius:4px;
  }
</style>
  <h2>Codeigniter Registration System</h2>
  <form method="POST" action="<?php echo base_url();?>index.php/blog/registration">
<table>
   <tr>
     <td>UserName:</td>
	 <td><input type="text" name="username"/>
	   <?php echo form_error('username','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
     <td>Password:</td>
	 <td><input type="text" name="password"/>
	   <?php echo form_error('password','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
     <td>Confirm Password:</td>
	 <td><input type="text" name="passconf"/>
	   <?php echo form_error('passconf','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
     <td>Email:</td>
	 <td><input type="text" name="email"/>
	  <?php echo form_error('email','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
     <td>First Name:</td>
	 <td><input type="text" name="fname"/>
	  <?php echo form_error('fname','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
     <td>Last Name:</td>
	 <td><input type="text" name="lname"/>
	  <?php echo form_error('lname','<div class=error>','</div>');?>
	 </td>
   </tr>
   <tr>
      <td></td>
	  <td><input type="submit" value="Sign Up"/></td>
   </tr>
</table>
</form>
<?php
if(isset($message)){
	echo $message;
}
?>
That's It How to Create Registration System in Codeigniter with Callback Function..
Keep visiting for More Codes and Don't Forget to Leave Your Comment's.
Thank You.

No comments:

Post a Comment

Thank You for Your Comment

Note: Only a member of this blog may post a comment.