5

Let's say I have a form on the add-project.html page. After the request is done, I redirect the user back to the main index.html page.

What I want to do is to display a notification on index.html only when the user has successfully submitted the form.

I am using this plug-in for the notification : http://redeyeoperations.com/plugins/Notify/

For now, I am using a temporary solution which shows a notification on add-project.html when the form has been submitted and then redirects to the main index.html page after a setTimeout but I don't like it, it's quite dirty.

$('#printForm').submit(function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'add-project.php',
        data: $('#printForm').serialize(),
        success: function(result) {
            if(result == '1') {
                // Success
                $.notify.basic('Congratulations, your projet has been saved correctly', {
                    occupySpace : true,
                    close : true,
                    autoClose: 5000
                });

                setTimeout(function() {
                    window.location.href = '../index.html';
                }, 2000);
            }
            else {
                // Error
                $.notify.error('Oops, something went wrong ! Make sure you filled all the necessary fields', {
                    occupySpace : true,
                    close : true,
                    autoClose: 5000
                });
            }
        }
    });         
});

Is there a clean way to show a notification on the target page after redirect?

Thanks for your help.

1
  • Why you not try to using ajaxLoad ?
    – Fredy
    Jul 11, 2012 at 7:50

1 Answer 1

5

index.html needs some way to know that the form was sent. You could do it by using an url parameter or hash and reading it with js.

//after submit
window.location.href = '../index.html#success';

//in index.html
if(window.location.hash == '#success')
{
    //show the notification
}
4
  • @Thomas, every reload of page will show notification. Is there way to show this notification just once?
    – Bagdat
    May 9, 2016 at 7:55
  • Thanks so much, this was exactly what I needed in my app! :)
    – Alexander
    Apr 12, 2018 at 13:41
  • @Bagdat I know it's a little late.. but you can empty the hash after displaying the notification. And I think that will work.
    – Alexander
    Apr 12, 2018 at 14:13
  • @Thomas, yeap, it is a little late) Nevertheless, thank you for your answer!
    – Bagdat
    Apr 13, 2018 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.