Welcome Guest, Not a member yet? Register   Sign In
How to check duplicate email? noob question.
#1

[eluser]anna16[/eluser]
Hi people

Sorry this is noob question.

For example i have a form.
Let say it has an email input field.
I want to get the value of that email and compare
it into the existing database.

It should check the duplicate of this email.
And should display an error message if the duplicate email is found.

Can you show me the controller, model and view.
Or a short example codes for this.
Sorry I'm a beginner.

Thanks in advance.
#2

[eluser]umefarooq[/eluser]
try to use CI form validation call back function it will check the email from database and will display your custom message to user, here is user guide how to create and call callback functions

http://ellislab.com/codeigniter/user-gui...#callbacks
#3

[eluser]Dennis Rasmussen[/eluser]
First of all there are no noob questions in this community.
You are asking for help and we will help, no matter the level of programming.

You can achieve what you want to do in a couple of ways.
1. Using the Form_validation library
2. Or do a manual check

Basically it's the same, however with the Form_validation library you can create rules of what has to be in the email field... even functions as rules (callback).

You can read more about Form_validation callbacks here:
http://ellislab.com/codeigniter/user-gui...#callbacks

In your case you basically want to create a function (method) in your class to use as a rule in your Form validation.

Modified from the documentation:
Code:
<?php

class Form extends Controller {
    
    function index()
    {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
            
        $this->form_validation->set_rules('email', 'Email', 'required|callback_email_unique');
                    
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
    function email_unique($str)
    {
        // Note that there are 100 of different ways to create this SQL,
        // so do it your own way.
        $result = $this->db->get_where('table_name', array('email' => $str));

        // Is there a row with this email?
        if ($result->num_rows > 0)
        {
            // Let's return false for the validation and set a custom message for this function
            $this->form_validation->set_message('email_unique', 'That email is already used.');
            return FALSE;
        }
        else
        {
            // Everything is good, don't return an error.
            return TRUE;
        }
    }
    
}
?>

I hope that helps Smile




Theme © iAndrew 2016 - Forum software by © MyBB