1

I'm trying to create a form validation callback function but I'm having a little trouble getting my head around it.

What I am trying to do is create a contact form where with a join the mailing list option. If the option to join the mailing list is checked I want the name and email of the person to be added to the mailing list database. This part works perfectly however I also want the function to check the database to ensure that the email address being added is unique and this is the bit that I just can't get my head around.

Controller:

public function contact()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name', 'your name', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));
    $this->form_validation->set_rules('email', 'your email address', 'required', array('required'=>"<p class='required'>Please provide %s</p><br>"));

    if($this->form_validation->run() == FALSE)
    {
        $this->load->view('templates/headder');
        $this->load->view('contact');
        $this->load->view('templates/footer');
    }
    else
    {
        $this->load->library('email');

        $name = $this->input->post('name');
        $email = $this->input->post('email');
        $phone = $this->input->post('phone');
        $message = $this->input->post('message');
        $list = $this->input->post('mailing_list');

        $email_message = "Name: $name<br>Email: $email<br>Phone: $phone<br>Message:<br>$message";

        $this->email->initialize();
        $this->email->from($email, $name);
        $this->email->to('[email protected]');
        $this->email->subject('New Query');
        $this->email->message($email_message);
        $this->email->send();

        if($this->email->send()){
        $this->load->view('send_error');
        }
        else
        {
            if($list == 'no')
            {
            $this->load->view('sent');
            }
            else
            {
                $this->form_validation->set_rules('email', 'Email', 'is_unique[mail_list, email]');


                if($this->form_validation->run() == FALSE)
                {

                $this->load->model('mailing_listm');
                $this->mailing_listm->add_name();
                $this->load->view('sent'); 
                }
                else
                {
                $this->load->view('contact');
                }

            }
        }
    }
}

Error Message:

A Database Error Occurred

Error Number: 1064

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' email '[email protected]' LIMIT 1' at line 3

SELECT * FROM `mail_list`, `email` WHERE mail_list, email '[email protected]' LIMIT 1

Filename: libraries/Form_validation.php

Line Number: 1134

Hopefully someone will be able to let me know what daft thing I've done this time.

Also, This function is turning into a bit of a monster, it's the most complicated thing I've every tried to write. Is there any way that I can split it out so that it is made up of several smaller functions instead of one gargantuan one?

Thanks,

EDIT I have updated my code in line with the comment below about using is_unique however now I am receiving an error message.

EDIT Model:

Public function add_name()
    {
        $this->name = $this->input->post('name');
        $this->email = $this->input->post('email');

        $this->db->insert('mail_list', $this);

    }

1 Answer 1

6

for checking unique field there is a validation rule in codeigniter.

is_unique[table.field]
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your response. I have updated my code (see my original post) but now I'm receiving an error when I submit a duplicate entry. Any idea why?
because you used comma(,) is_unique[mail_list, email] after your table name. you should use dot(.)is_unique[mail_list.email] without any space between table and column name.
D'oh! ok, I've corrected that but now I have yet another error "A Database Error Occurred, Error Number: 1062, Duplicate entry 'Tessa' for key 'name_2', INSERT INTO mail_list (name, email) VALUES ('Tessa', '[email protected]'), Filename: C:/xampp/htdocs/mywebsite/CI/application/models/mailing_listm.php, Line Number: 16."
why you validating your email for two times! why not at the top of your controller!
because I want to validate that a email address has been provided before the contact email is sent but I only want to validate that it's unique if the person wants to become part of the mailing list.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.