Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

unitedworx's avatar

Problem with Ajax form post : on success redirect and show message

I have been converting my forms to submit via Ajax which is very convenient with validation since I get back validation error messages and displayed client side without the usual post form and the reshow the form with entered data.

On success I show a success message and this works well however If I want to redirect my user to the previous page and then show the success message I can't coz the messages are stored in the session and not sure how to best handle this client side.

Effectively I want to do the same the below code but with jquery/javascript

Redirect::to('my previous page')->with('message.success', 'some message here');

I can handle the redirection just fine however I am having problems storing the message in the session so it's displays on the next page I am redirected to.

Any help will be much appreciated

0 likes
5 replies
unitedworx's avatar

Thanks for the reply pobble but I wanted to keep thigns consistently with my laravel flash messages so local storage was not an option.

Here is how ended up doing this if anyone else ends up with the same problem! i basically make another ajax call to set the success message and then redirect

in javascript

function redirectWithFlashMessage ( redirect_url , status, message ) {
    $.ajax({
        type: 'POST',
        url: 'admin/flash',
        data: { 'status' : status , 'message' : message }
    }).done(function () {
        window.location.href = redirect_url;
    });
}

in routes.php

// admin ajax call to set flash message via javascript
Route::post('admin/flash', function (){
    Session::flash('message.' . Input::get('status'), Input::get('message') );
    return ['status' => Input::get('status'), 'message'=> Input::get('message')];
});
1 like
srkiranraj's avatar

@unitedworx - You wouldn't need 2 ajax calls, check the following solution & let me know if its good/bad ?


$.ajax({
    url: url,
    data: data,
    success: function(data){
        if(data == "success")
            window.location.href = redirect_url;
        else
            console.log("Error");
    }
});


But the twist is in controller that handled the first ajax,


Route::post(url, function(){
    // Do all validation
    if(valid)
    {
        Session::flash('key', 'value');     // Flash just before your success.
                                // When reloaded/redirected in your ajax success,
                                // you will be able to retrieve this flashed key in the next request.
        return "success";
    }
    else
        return "failure";
});
unitedworx's avatar

i thought of doing that as well , but i considered it a bad solution since i didn't want an ajax called method that performed action X or Y to flash a message to the session since i might want to call that method from somewhere else.

having a route specially for setting flash messages and a corresponding javascript function has been very handy since i can control from with-in my javascript my success or error messages which is more readable and clean in my opinion.

1 ajax call more does not hurt :)

Kryptonit3's avatar

If you call this in your controller

Session::flash('success_msg', 'Success!');

and then have your javascript set to redirect, it doesn't matter what page they are redirected to, they will see this message. Make sure you have something like this in the area you want to display notices. (usually a global view like navbar)

 @if(Session::has('error_msg')
    <div class="alert alert-dismissable alert-danger">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('error_msg')}}
    </div>
 @endif

@if(Session::has('success_msg')
    <div class="alert alert-dismissable alert-sucess">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('success_msg')}}
    </div>
 @endif

This way, you do not have to keep setting up a place to display error or success messages. they will always show on whatever page you redirect the user to. As long as you throw a session flash before the redirect.

So in the controller, the only variable that changes is if it is an error (red) message, or success (green) message. You could even do warning (yellow/orange) and notice (blue) messages this way as long as you code the global view part to look for it.

Session::flash('success_msg', '<strong>Success!</strong> You must now verify your email!');
Session::flash('error_msg', '<strong>Oh snap!</strong> You have entered an invalid email!');
Session::flash('warning_msg', '<strong>Look out!</strong> It appears your account has been logged in from another location!');
Session::flash('notice_msg', '<strong>Heads up!</strong> It looks like your subscription will be expiring soon! <a class="alert-link" href="' . URL::route('updateSubscription') . '">Click here</a> to update it!');

Then

@if(Session::has('error_msg')
    <div class="alert alert-dismissable alert-danger">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('error_msg')}}
    </div>
 @endif

@if(Session::has('success_msg')
    <div class="alert alert-dismissable alert-sucess">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('success_msg')}}
    </div>
 @endif

@if(Session::has('warning_msg')
    <div class="alert alert-dismissable alert-warning">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('warning_msg')}}
    </div>
 @endif

@if(Session::has('notice_msg')
    <div class="alert alert-dismissable alert-info">
        <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
        {{Session::get('notice_msg')}}
    </div>
 @endif

http://getbootstrap.com/components/#alerts

4 likes

Please or to participate in this conversation.