Nicolai Lissau
Nicolai Lissau

Reputation: 8312

Codeigniter redirect does not update url using jQuery mobile

I have a login controller, which is suppose to redirect to my index page when the user is valid. The redirect works, but at the index page the url is still that of the validation method ex: login/validate_login/. If i click on a link on the index page, and then try and go back in the brower history, the browser points me to the validate method and not the index page.

How do i fix this?

I have tried using redirect with both refresh and location, but both with no luck.

I suspect this is a problem with the ajax call of jQuery mobile, but i'm not sure.

Any help appreciated. Kind regards

NOTE: I would post an image of the url at the index page, but i'm not allowed to because i'm a new user.

My validate method:

    function validate_login($controller='',$method='') {
    $this->load->library('form_validation');
    $this->load->model('workout_model');

    $this->form_validation->set_rules('ex_password','Koden','trim|required|min_length[4]|max_length[4]|callback_pw_check');


    if($this->form_validation->run() == FALSE) {
        $this->index();
    } else {
        if($query = $this->workout_model->validate()) {

            $data = array(
            'is_logged_in' => true,
            'user_id' => $query->id,
            'current_exercise' => '1'
            );

            $this->session->set_userdata($data);

            if($controller=='' || $method=='') {
                redirect("workout");
            } else {
                redirect($controller."/".$method);
            }
        } else {
            $this->index();
        }
    }
}

Upvotes: 3

Views: 1220

Answers (3)

Mewan
Mewan

Reputation: 39

when you submit via HTML form, such as login register .then you have also some kind of dashboard redirection, you need to update your form tag like this.this occurs when I'm using Codeigniter and jquery mobile use data-ajax="false"

<form id="login-box" method="post" action="<?php echo base_url('index.php/auth/login_user'); ?> " data-ajax="false">

Upvotes: 0

Manu Torres
Manu Torres

Reputation: 68

Two things seem to be necessary for the URL to be correctly updated: use redirect(...) instead of method calls AND disable jQuery Mobile ajax call. Two ways of doing that: add and attribute to the link that initially points to your method,

<a href='.../validate_login/...' data-ajax='false'>...</a>

or, disable ajax calls globally by editing the settings (not tested).

Upvotes: 1

Rick Calder
Rick Calder

Reputation: 18695

There are two ways to handle this and it has nothing to do with AJAX it's the way CI does things. The quickest and easiest way is to change

$this->index();

to

redirect(index);

The way you're doing it you're not actually redirecting, you're calling the index function on the current URL which is validate_login. The problem with doing it this way is if the login fails it will still remain on the validate_login URL for the next try.

The best way to handle it is to have the actual validate_login function called from your index function in the controller rather than the form itself. So send the form back to index, have the index controller check for the form data and if true call validate_login(). That way you're never actually leaving the index page, it just handles whether or not the login form has been submitted. Solving the URL issue. I actually do this with all my pages that submit forms for any kind of validation.

Upvotes: 0

Related Questions