3

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();
        }
    }
}
Nicolai Lissau
  • 7,298
  • 5
  • 43
  • 57
  • [This](http://stackoverflow.com/a/12486869/568884) may seem like a little bit of overhead right now, but you'll thank me later. – Jordan Arsenault Oct 12 '12 at 17:46
  • Very nice post. However this is only a small application for logging my fitness workout, so I think I will experiment with this some other day. This problem is pretty annoying, since it is the only thing left for the "beta" version... – Nicolai Lissau Oct 12 '12 at 21:27

3 Answers3

1

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).

Manu Torres
  • 68
  • 2
  • 9
0

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.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • Thank you for your reply. I can't get this to work.. Changing the $this->index(); does not work, and neither did it to put the code of validate_login() into the index(). Perhaps I were not clear enough. I have two controllers one workout and the other workout_login. In the constructor of workout I call a method called is_logged_in() which checks if userdata is set. If not, i redirect to the workout_login controller. Kind regards – Nicolai Lissau Oct 16 '12 at 16:17
  • @Rick Calder what about validation errors as when redirected the errors would be lost – Harsh Gundecha Apr 28 '18 at 13:03
  • lol, 6 year old answer >.< if you have errors you can just follow the docs for redirecting with the errors and previous formdata present. https://www.codeigniter.com/userguide3/libraries/form_validation.html – Rick Calder Apr 28 '18 at 18:22
0

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">
Mewan
  • 29
  • 4