2012-07-11 22 views
1

比方说,我在add-project.html页面上有一个表单。请求完成后,我将用户重定向回主页面index.html页面。重定向后在目标页面上显示通知

我想要做的是在用户成功提交表单时显示通知index.html只有

我的插件使用本作的通知:http://redeyeoperations.com/plugins/Notify/

现在,我使用的是临时的解决方案,它显示了add-project.html通知当表单已经提交,再经过重定向到主index.html页一个setTimeout但我不喜欢它,它很脏。

$('#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 
       }); 
      } 
     } 
    });   
}); 

有没有干净的方式显示在目标页面上的通知重定向之后?

感谢您的帮助。

+0

为什么不尝试使用ajaxLoad? – Fredy 2012-07-11 07:50:46

回答

2

index.html需要一些方法来知道表单已发送。你可以通过使用url参数或散列并使用js读取它。

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

//in index.html 
if(window.location.hash == '#success') 
{ 
    //show the notification 
} 
+0

非常好,那就是我一直在寻找的东西。谢谢 ! – morgi 2012-07-11 08:06:02

+0

@Thomas,每页的重新加载都会显示通知。有没有办法只显示一次这个通知? – Bagdat 2016-05-09 07:55:00

相关问题