Adi Satria
Adi Satria

Reputation: 93

redirect not work properly in codeigniter

Halo, i'm using ajax to post form into controller codeigniter. I want to redirect after ajax post, but controller doesn't redirect.

This is my ajax

    $.ajax({
        type:"POST",
        url:form.attr("action"),
        data:form.serialize(),

        success: function(){

         },
        error: function(){
        alert("failure");
        }
    });
  });
});

this is my controller

public function checkout_data(){
    $this->account_model->checkout_simpan();
    redirect('produk/payment/last_steps');
}

this is my form

<form class="form-horizontal col-md-offset-3" id="form-checkout" action="<?php echo base_url('produk/payment/checkout_data');?>">

What wrong with my code ?

Upvotes: 1

Views: 440

Answers (2)

Devang Hire
Devang Hire

Reputation: 314

1 ) Check your path properly 2 )(important) Check Your Controller there is unwanted space in it remove all these

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

You're doing it wrong.

What are you doing now: when you send ajax-request to your server, method checkout_data is executed, and in it there's a redirect to another url. But it works on server. So, after redirecting to produk/payment/last_steps, method last_steps (or whatever is binded to this url) is executed and it's contents returned back to ajax-request.

What you need to do:

use javascript functions to redirect. Usually it's a document.location E.g. document.location = "some/new/url". So I suppose your checkout_data method should return some string. that contains url for redirect. For example:

public function checkout_data(){
    $this->account_model->checkout_simpan();
    echo 'produk/payment/last_steps';
}

And in success of ajax you can use:

success: function( data ) {
    // console.log( data ) // uncomment to check what is received
    document.location = data; 
},

Upvotes: 1

Related Questions