0

I have a register page in which via ajax request i want to redirect the user to the logged in page. But, this particular ajax request does a redirect but doesn't pass on the session after the POST request

 var dataString = 'name='+ name + '&email=' + email1 + '&password=' + password1;
                       //alert (dataString);return false;
                       $.ajax({
                           type: "POST",
                           url: "http://localhost/ci/index.php/login/add/",
                           data: dataString,
                           success: function() {
                            window.location.href = "http://localhost/ci/dashboard";
                                                }
                       });

The login/add function is taking the values via POST request which then puts these value in session and does a redirect. The same thing is happening perfectly without a ajax request in plain php/CI. But, not happening via ajax request.

Here's the php code if it helps

    if ($this->input->post("add") == "Sign up") {
        $data_array = array(); //this array contains post data
        $output = $this->user_model->add_new($data_array);
        if($output == TRUE){
                $this->session->set_userdata('logged_in', '1'); 
        }

        $data = array(
                    'name' => form_error('name'), 
        'email' => form_error('email'), 
        'password' => form_error('password')    
        );
        echo json_encode($data);

}

gauravtechie
  • 95
  • 11
  • I would say change your DataString to actual post syntax like `data: ({"name":name, "email":email, "password":password})` assuming those are collected as JS variable available to this ajax function. This is as a side note, not solution to your problem. But could end up fixing it, if your values contain those special character such as ' or &. – Tumharyyaaden Aug 24 '11 at 19:50
  • @Tumharyyaaden: You don't need the `()` around the object. `data: {"name":name, "email":email, "password":password}` – gen_Eric Aug 24 '11 at 19:55
  • @Rocket: that's good to know, so just to double check, even if lets say you have strings, JS variables and or arrays, you can still leave () out? Might be dumb que but i thought adding () was "safer". – Tumharyyaaden Aug 24 '11 at 20:07
  • @Tumharyyaaden: Adding `()` around an object does nothing in JavaScript. `({a:'a', b: [1,2]})` is the same as `{a:'a', b: [1,2]}`. – gen_Eric Aug 24 '11 at 20:10
  • @Rocket: that's not entirely (does come in handy with groups of group) true but i got it. – Tumharyyaaden Aug 24 '11 at 20:14
  • Include your whole controller looks like, also the related model. If somehow, php error occured, then indeed your AJAX will fails. – toopay Aug 25 '11 at 12:15

1 Answers1

1

The login/add function is taking the values via POST request which then puts these value in session and does a redirect. The same thing is happening perfectly without a ajax request in plain php/CI. But, not happening via ajax request.

It because previously you perform a synchronous request, while with jQuery ajax you are perform asynchronous request(this to say that your javascript is may ignore some part of your PHP script). Try adding async parameter in your ajax and set it to false

toopay
  • 1,635
  • 11
  • 18
  • I just double check, and it works here. It can be PHP error occured, and lead your AJAX fails... – toopay Aug 25 '11 at 12:34