Welcome Guest, Not a member yet? Register   Sign In
Converting to AJAX
#8

[eluser]Colin Williams[/eluser]
Maybe this will help. These are snippets of code from a recent signup form I did that validated things as the user moved through the form.

First, here's the javascript. Basically it is performing an Ajax request to see if the email provided is not yet in use.
Code:
// Special checks on email field
var El = $('#join-form #edit-email');
if (El.length > 0) {
    El.blur(
        function() {
            // Is the email address already being used?
            $.post('/people/check_email',
                {email : El.val()},
                function (data) {
                    if (data.is_user) {
                        El.next('.control-message').remove();
                        El.after('<span class="control-message control-error">'+ data.message +'</span>');
                        validEmail = false;
                    }
                    else if ( ! data.is_valid) {
                        El.next('.control-message').remove();
                        El.after('<span class="control-message control-error">'+ data.message +'</span>');
                        validEmail = false;
                    }
                    else {
                        El.parent().find('.control-error').remove();
                        validEmail = true;
                    }
                },
                'json'
            );
        }
    );
}

The POST is hitting the 'people' controller and the 'check_email' function, which looks like this:
Code:
function check_email()
{
    $this->load->library('validation');
    $result = array(
        'is_user' => FALSE,
        'is_valid' => TRUE,
        'message' => ''
    );
    if ($this->users->user_exists(array('email' => $this->input->post('email'))))
    {
        $result['is_user'] = TRUE;
        $result['message'] = 'This email is already in use. Use another.';
    }
    if ( ! $this->validation->valid_email($this->input->post('email')))
    {
        $result['is_valid'] = FALSE;
        $result['message'] = 'This email is not a valid address.';
    }
    print json_encode($result);
}

Maybe you can follow that and glean some ideas from it


Messages In This Thread
Converting to AJAX - by El Forum - 07-26-2009, 02:12 PM
Converting to AJAX - by El Forum - 07-26-2009, 02:25 PM
Converting to AJAX - by El Forum - 07-26-2009, 02:32 PM
Converting to AJAX - by El Forum - 07-26-2009, 02:34 PM
Converting to AJAX - by El Forum - 07-26-2009, 03:15 PM
Converting to AJAX - by El Forum - 07-26-2009, 07:48 PM
Converting to AJAX - by El Forum - 07-26-2009, 09:20 PM
Converting to AJAX - by El Forum - 07-26-2009, 09:36 PM



Theme © iAndrew 2016 - Forum software by © MyBB