CodeIgniter Forums
How to redirect on form validation succes? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to redirect on form validation succes? (/showthread.php?tid=26248)

Pages: 1 2


How to redirect on form validation succes? - El Forum - 01-10-2010

[eluser]kgill[/eluser]
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.


How to redirect on form validation succes? - El Forum - 01-10-2010

[eluser]Dyllon[/eluser]
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.

Untested:
Code:
function process() {
    $this->load->helper('jsonwrapper');
    $this->load->helper('url');
    $this->load->library("validation");
    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);          
    }

}

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



How to redirect on form validation succes? - El Forum - 01-11-2010

[eluser]123wesweat[/eluser]
@dyllon, tx i am going to try your suggestion.

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

document.location="mydomain.com"


How to redirect on form validation succes? - El Forum - 01-11-2010

[eluser]Dyllon[/eluser]
yes but I used window . location