Most of the time people ask to implement username or email is already exist or not while user registration of functionality. Its very easy to implement using codeigniter and this time i am using codeigniter library. You may also like How to generate random username using PHP, Ajax and MySQL and How to Check UserName and Email Availability From Database using PHP and Ajax.
Steps For How to Check Availability of the Username and Email using Codeigniter
Step 1. Connect To Library File
In this step we have go to application/config/autoload.php and set libraries.
$autoload['libraries'] = array('database','session','form_validation');
Step 2. Create View
In this step we have create new view file like.
public function Register()
{
$this->load->view('register');
}
In above view load inside application/views/register.php
<form action="" class="form-horizontal" id="register_user" method="post" enctype="multipart/form-data">
<div class="col-md-6">
<div class="form-group">
<div class="col-md-12">
<span style="color: #ffffff;">First Name <span style="color: #999999;">*</span></span>
<input type="text" style="margin-top: 5px;" class="form-control" value="<?= set_value('first_name'); ?>" required="" name="first_name" placeholder="First Name" />
<div class="error"><?php echo form_error('first_name'); ?></div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<span style="color: #ffffff;">Last Name <span style="color: #999999;">*</span></span>
<input type="text" style="margin-top: 5px;" class="form-control" value="<?= set_value('last_name'); ?>" required="" name="last_name" placeholder="Last Name" />
<div class="error"><?php echo form_error('last_name'); ?></div>
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<span style="color: #ffffff;">Email <span style="color: #999999;">*</span></span>
<input type="email" style="margin-top: 5px;" class="form-control" value="<?= set_value('email'); ?>" required="" name="email" placeholder="Email" />
<div class="error"><?php echo form_error('email'); ?></div>
</div>
</div>
<div class="form-group" style="float: right;">
<div class="col-md-12">
<button class="btn btn-info">Register</button>
</div>
</div>
</div>
</form>
In above form submit and check email or username in database.
Step 3. Form Submit and Check username or email in database
In this step we have use form_validation library and easy to check in database. username or email already available in database form validation false and automatic go view file and generate error.
public function Register()
{
if(!empty($_POST))
{
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[tbl_user.email]');
if ($this->form_validation->run() == true)
{
$array = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email' => $this->input->post('email'),
);
$this->db->insert('tbl_user',$array);
$this->session->set_flashdata('SUCCESSMSGREGISTER','Account Successfully Created.');
redirect('login');
}
}
$this->load->view('register');
}
And if you like this tutorials please share it with your friends via Email or Social Media.