简体   繁体   中英

How to redirect on a successful AJAX form submission (PHP - CodeIgniter)

I have a registration form that is being sent to the controller for validations, it is being sent back to the view page with json encode(); on the view page this is my script for ajax:

$('#register_form').submit(function(){
    $.post(    
        $(this).attr('action'),
        $(this).serialize(),
        function(data){
            $('.prev_message').parent('div').removeClass('field_block');
            $('.prev_message').html('');
            if(data.register_error)
            {
                $('#messages_register_error').html(data.register_error);
            }
            if(data.email)
            {
                $('#messages_email').parent('div').addClass('field_block');
                $('#messages_email').html(data.email);
            }
            if(data.first_name)
            {
                $('#messages_first_name').parent('div').addClass('field_block');
                $('#messages_first_name').html(data.first_name);
            }
            if(data.last_name)
            {
                $('#messages_last_name').parent('div').addClass('field_block');
                $('#messages_last_name').html(data.last_name);
            }
            if(data.password)
            {
                $('#messages_password').parent('div').addClass('field_block');
                $('#messages_password').html(data.password);
            }
            if(data.confirm_password)
            {
                $('#messages_confirm_password').parent('div').addClass('field_block');
                $('#messages_confirm_password').html(data.confirm_password);
            } 
            // if(data.link)
            // {
                // return true;
            // }
        },                    
        "json"        
    );
    return false;
});

I would like to know how I can have the page redirect to another page if there are no errors returned from the controller. This is the part of code in the controller that is supposed to redirect to another page on successful submission:

if($this->logged_user_info->user_status != 'admin')
{
    //set the dashboard link to user_dashboard
    $this->page['dashboard_link'] = "/ci/dashboard";
    $this->session->set_userdata('dashboard_link', $this->page['dashboard_link']);
    //go to user_dashboard function which will login user and display user dashboard info
    // $dashboard_link['link'] = 'success';
    // var_dump($dashboard_link);
    // die();
    // echo json_encode($dashboard_link);

    // redirect(base_url('/dashboard'));
    header('Location: user_dashboard');
}

Right now all that happens when I submit the form successfully - it stays on the register view page, and when I inspect element I notice that it is going to the dashboard page but only in the inspect element section not on the view page. (I have a feeling it has to do with the fact that in my ajax code it says return false; ) I am just not sure how to go about that it should only "return false" when there are errors?

(The commented out code is the code I was trying to insert to fix this problem, by sending back to the view page a message for success but then I was still having trouble making that the ajax code should redirect to a second view page on success message returned.)

Don't submit a redirect header but the URL and trigger the redirect at the client: window.location.href = target (you almost had it btw):

if(data.link) {
    window.location.href = data.link;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM