2012-07-11 29 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

相關問題