Welcome Guest, Not a member yet? Register   Sign In
AJAX form not redirecting
#1

[eluser]cmgmyr[/eluser]
Hey all,
I'm new at CI (and to the forum) so sorry for the noobish questions.

The view:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
[removed][removed]
[removed][removed]
[removed]
function sendForm(container) {
    var url = '&lt;?=base_url()?&gt;admin/home/login';
    var params = Form.serialize(container);
    var ajax = new Ajax.Request(url,{
        method: 'post',
        parameters: params,
        onComplete: showErrors
    });
}
function showErrors(req){
    if(req.responseText == 'OK'){
        location.href='&lt;?=site_url("admin/catalog")?&gt;';
    }else{
        $('error_messages')[removed] = req.responseText;
        new Effect.Appear('error_messages');
    }
}
[removed]
&lt;/head&gt;

&lt;body&gt;
<div id="error_messages" style="display:none;"></div>
&lt;form id="frm_login"&gt;
<p>&lt;input id="email" name="email" type="text" /&gt;&lt;/p>
<p>&lt;input id="password" name="password" type="text" /&gt;&lt;/p>
<p>&lt;input name="submit" type="submit" value="Login!" /&gt;&lt;/p>
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

The Controller:
Code:
function login(){
        $email = $this->input->post('email');
        $password = $this->input->post('password');
        $logged_in = $this->doLogin($email, $password);
        
        if($logged_in){
            //echo 'OK';
            //$this->func->pageForward(site_url("admin/catalog"));
            redirect(site_url("admin/catalog"), 'refresh');
        }
    }
    
    function doLogin($user_email, $user_password){    
        if(!$this->val->check_email($user_email)){
            $this->error->setError("Email address is not valid");
        }
        
        if(!$this->val->check_input($user_password)){
            $this->error->setError("Password is not valid");
        }
        
        if(!$this->error->countErrors()){
            //Check the database for correct information
            $sql = "SELECT * FROM admin WHERE email = '".$user_email."' LIMIT 1";
            $query = $this->db->query($sql);
            
            if($query->num_rows() == 0){
                $this->error->setError("Email address not found");
            }else{
                //Check for both email and password
                $row = $query->row();
                $db_password = $row->password;
                
                if(!$this->func->validate_password($user_password, $db_password)){
                    $this->error->setError("Password doesn't match database");
                }else{
                    $id = $row->id;
                    
                    //See if user account is active or not
                    $sql = "SELECT * FROM admin WHERE id = '$id' AND active = '0'";
                    $query = $this->db->query($sql);
                    
                    if($query->num_rows() > 0){
                        $this->error->setError("Your account is disabled, this might be becuase your account is not approved yet or you have violated the TOS.");
                    }
                    
                    if(!$this->error->countErrors()){
                        //Log user in
                        $newdata = array('admin_id' => $id);
                        $this->session->set_userdata($newdata);
                        
                        return true;
                    }
                }
            }
        }
        
        if($this->error->countErrors()){
            $this->error->showErrors();
        }
        
        return false;
    }

As you can probably see in the JS the redirect in the showErrors() does work BUT I would rather do the redirect in the controller. How it is set up now it shows admin/catalog in the "error_messages" div. Any ideas how I can redirect the whole page and not just the div?

Thanks,
-Chris
#2

[eluser]cmgmyr[/eluser]
Any ideas?
#3

[eluser]mdowns[/eluser]
All the AJAX is doing is making a request and returning the response. Your javascript currently updates the error_messages div with the response, making it seem like the div is 'redirecting'.

If you want to have the redirect in the controller, just use a normal form with method='post' and action='admin/home/login'.
#4

[eluser]cmgmyr[/eluser]
Thanks for the response. I know that I could change the action of the form and have the page "refresh" then check the form, but that would kind of defeat the point of having an ajax form wouldn't it?
#5

[eluser]mdowns[/eluser]
Absolutely. But why use AJAX if you require a page load anyways...?
#6

[eluser]cmgmyr[/eluser]
Well the page load is only supposed to happen if the login is successful. I guess I can alter it to make the whole section be ajax in a div. I'll have to play with it more I guess.
#7

[eluser]mdowns[/eluser]
Maybe try something like this:

Code:
<div id="error_messages">
&lt;? if(!empty($redirectUrl)): ?&gt;
[removed]
location.href = '&lt;?= $redirectUrl ?&gt;';
[removed]
&lt;? endif; ?&gt;
</div>

Then in your login controller you would pass the redirectUrl into the view:
Code:
function login()
{
  $data['redirectUrl'] = 'myredirecturl.com';
  $this->load->view('myview',$data);
}

Good luck




Theme © iAndrew 2016 - Forum software by © MyBB