• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to redirect on form validation succes?

Ok, if you're using .post() you're sending the form via an AJAX request - you can't redirect that. Redirection works like this, the browser sends a request to the server saying give me this page, the server responds with go here instead. If nothing has loaded in the browser yet the browser makes the request for that page. This is why if you accidentally echo/print/have an error before a redirect it fails.

Where you're trying to redirect, the browser has already rendered a complete page and is executing scripts on it, the server can't force the client to go somewhere else at that point. Your solution is simple, either submit the form normally or alter your JS to use a callback that sets the window location.

In your process function you are successfully returning validation errors with json, you need to return a success message the same way with json.
Then in your jquery callback function check your data to determine if it's validation errors or a saved record and redirect or display errors as necessary.

function process() {
    if ($this->validation->run() == FALSE) {
        $data = array(
                'fname' => $this->validation->fname_error,
                'lname' => $this->validation->lname_error
        echo json_encode($data);
    } else {
        $data = array(
                'fname' => $this->input->post("fname"),
                'lname' => $this->input->post("lname"),
                'success' => TRUE
        echo json_encode($data);          


$(document).ready(function() {
    $("#form").submit(function() {
        var fname= $("#fname").val();
        var lname= $("#lname").val();
        $.post("/welcome/process", { fname:fname, lname:lname },
            if(data.success == TRUE) {
                [removed] = BASE_URI + 'welcome';
            else {

@dyllon, tx i am going to try your suggestion.

I think something is [removed] within your code. Would that be document . location ???


yes but I used window . location

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.