We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Redirection problem after using ajax to POST and model save

Hi, I have a basic sign up form and my javascript is as follow:

$("#signUpButton").click(function() {
    var confirmation = $('#rform-confirmation').is(':checked');
    //console.log(confirmation);

    if(confirmation) {
        var firstname = $("#rform-firstname").val();
        var lastname = $('#rform-lastname').val();
        var email = $('#rform-email').val();
        var password = $('#rform-password').val();
        var rpassword = $('#rform-rpassword').val();
        var tokenKey = $('#rform-csrf').attr('name');
        var token = $('#rform-csrf').val();

        console.log(tokenKey);
        console.log(token);

        //debugger;

        $.ajax({
            url: '/register/doRegister',
            type: 'POST',
            data:{
                firstname: firstname,
                lastname: lastname,
                email: email,
                password: password,
                rpassword: rpassword,
                tokenKey: tokenKey,
                token: token
            },
            success: function(res) {
                console.log(res);
            }
        });
    } else {
        alert('Please agree to the Terms of Service.');
        return false;
    }
});

Now I have this in my controller:

$send = $user->addUser($email, $pwd, $fname, $lname);
if($send){
     $this->flash->success('You have registered successfully');
     $this->response->redirect('/signin');
}

The functionality works fine (ie. it saves into the model and everything else) but it just reloads the page and somehow I can see that in the url bar in the browser shows the information of the POST. The flash message will come after I physically clicked on another link. I tried doing different redirect path in $this->response->redirect('signin'); or $this->response->redirect($this->get->url('signin'); still does not work. Though if I use the same redirection in other function they work perfectly.



17.4k
Accepted
answer
edited Aug '15

You can't redirect in an AJAX request.

I would return true (or success) in the controller and inside of the success function in your AJAX request do a location.href = "/signin"

edited Aug '15

I see. Thanks alot. But the flash message will not show up.

You can't redirect in an AJAX request.

I would return true (or success) in the controller and inside of the success function in your AJAX request do a location.href = "/signin"

edited Sep '15

Bryan you can send back a JSON object to your javascript and then do stuff off of that.

Controller

echo json_encode(['status' => 'success', 'message' => 'a message here']);

Javscript

$.post('/action/',{data: data}, function(response){
if (response['status'] == 'sucess'){
    window.location = "http://www.google.com/"
} else{
    alert (response['message']);
}),'json');

Oh thanks for the solution, but I ended up going back to handle the form with volt and php because of my flash message.

Bryan you can send back a JSON object to your javascript and then do stuff off of that.

Controller

echo json_encode(['status' => 'success', 'message' => 'a message here']);

Javscript

$.post('/action/',{data: data}, function(response){
if (response['status'] == 'sucess'){
  window.location = "http://www.google.com/"
} else{
  alert (response['message']);
}),'json');