0

I have searched around here in stackoverflow and to websites in general, but after trying to apply every fix, it doesn't seem to work (validation doesn't appear in my form). I'm using this library (jqueryvalidation.org) for my form and using the remote feature that connects to a controller which links to the model containing the database check.

See the code below and reply back if anything is not clear :)

Functions Controller (email_exists method) - /application/controllers/Functions.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Functions extends CI_Controller {
    public function index() {
        show_404();
    }

    public function email_exists() {
        if (isset($_POST['email'])) {
            $this->load->model('account_model');

            if ($this->account_model->email_exists($this->input->post('email')) == TRUE) {
                echo 'TRUE';
            } else {
                echo 'FALSE';
            }
        }
    }
}

Account Model (email_exists method) - /application/models/Account_model.php:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Account_model extends CI_Model {
    public function email_exists($email) {
        $query = $this->db->select('email_address')->get_where('accounts', array('email_address' => $email));

        if ($query->num_rows() == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

jQuery Validation Rules (part)

rules: {
    'email_address': {
        required: true,
        email: true,
        remote: {
            url: "/functions/email_exists",
            type: "post",
            data: {
                email: function() {
                    return $("#email").val();
                }
            }
        }
    }

Form Input (email section - part of the file):

$input_email_address = array('name' => 'email_address',
                             'type' => 'email',
                             'id' => 'email_address',
                             'class' => 'form-control',
                             'value' => $this->input->post('email_address'),
                             'autofocus' => 'autofocus');
echo form_input($input_email_address);

Any help would be really appreciated, I have been playing with it since the morning but no luck. Thank you in advance :)

2 Answers 2

1

The selecter is not the same as the input field.

#email != #email_address

rules: {
    'email_address': {
        required: true,
        email: true,
        remote: {
            url: "http://YOUR_URL",
            type: "post",
            data: {
                email: function() {
                    return $("#email_address").val();
                }
            }
        }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Please remove link if possible, forgot it there! ;) Thanks very much for your help, that's right!
I deleted the URL. :)
0

I better say use jQuery.validator.addMethod('isEmailExists'....

And rules like

email: { required: true, email: true, isEmailExists: true },

Comments

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.