views:

324

answers:

1

EDIT:

I found the problem with my evaluation not working. All the text being returned from my codeIgniter functions has a space before it, so while I saw "success" it was actually " success". I don't know why it is, but I can certainly work with that.

As for the next step - opening a new view - Donny's answer was perfect!


I am using CodeIgniter for an application with JQuery for my AJAX library. I'm just learning the AJAX stuff, so I'm probably missing something basic here...

The following code if for a login form.

The goal is this -use an ajax call on the form submit so I can validate the errors and provide error messages on screen without web page refreshes. I'm doing all my validation with the CodeIgniter form_validation class.

My codeigniter function returns a text value - either an appropriate error message or the word "success." I want to evaluate the text value, and if it says "success", call another ajax function to load the needed CodeIgniter function that will load home page for logged in users.

Right now everything works in the code below until I get to the statement "if data=='success'".

Why would that return false when I know it is true because the on screen message displays "success"?

$(document).ready(function() {
    $('#registration_form').submit(function(e) {
        e.preventDefault();
        $.post("login", {
            email : $('#email').val(),
            password : $('#password').val()
        }, function(data) {
            $('#message').text(data);

            if (data == "success") {
                alert(data);
                post("landingpage");
            }
        });
    });
});
+1  A: 

You should consider using jQuery form plugins to do form submit using AJAX.

To do redirection, use this code:

window.top.location.assign(URL);

URL is the page destination. If you use CI, you can put this in view:

window.top.location.assign("<?php echo site_url(THE_PAGE_URL); ");

also, you can examine the data value in the Firebug net tab, or in Web Inspector Resource tab (Safari and Google Chrome).

Donny Kurnia
thanks for the advice. Quick question - what is the particular advantage of the form plugins? I will look at them.
sql_mommy