2011-11-29 103 views
0

我使用ajax動態獲取數據,但存在問題。當我刷新頁面時,獲取的數據會丟失。我想刷新頁面後數據保持不變。從ajax提取數據後重新加載頁面

想這是我的網頁:

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Index Page</title> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> 
<script type="text/javascript"> 
/* fetching data which matches the keyword*/ 
function Search() 
    { 
    $.post("search.php", {search_word: $('#searchInput').val()} , function(data){ 
    if(data){ 
     $('#results').show();     
     $("#results").append(data);     
    } 
    }); 
    } 
</script> 
</head> 

<body> 
<input type="text" value="" id="searchInput"/> 
<input type="button" value="Search" onclick="Search();"/> 
<div id="results" style="display:none"></div> 
</body> 
</html> 

當我上的搜索按鈕,點擊它排在結果DIV刷新頁面後,就會消失的數據。

+1

您應該向社區提供一些您的代碼,以便我們可以看到您要做的事情併爲您提供詳細的幫助。 – sirmdawg

+0

假設有一個div的id爲demoDiv,並且來自搜索頁面的響應數據出現在這個div中,現在當我刷新頁面時,這個div數據將消失...但我希望在重新加載頁面後數據將保留在此處。 。這對你來說是可以理解的,現在我的問題是什麼? –

+0

是的,你可以編輯你的問題,包括你使用的示例代碼?也許阿賈克斯加載部分? – sirmdawg

回答

1

保存在sessiondatabase,然後數據從那裏取 因爲清爽頁面getpost變量後自動銷燬。

+0

是否有任何其他選項insted會話和存儲在數據庫中??? –

+0

是的,當你得到的內容,然後再次張貼在同一頁上的價值一次又一次 –

1

考慮使用cookie數據。

這個jQuery插件將這樣的伎倆: http://plugins.jquery.com/project/cookie

$.cookie('example', 'foo'); //the name is example and that value is foo. 

獲取cookie的值是簡單...

alert($.cookie('example'); 

加載數據,以便動態地後,你既可以存儲數據在一個cookie中。儘管如果您獲取大量數據,這可能是一個不好的解決方案。

這個問題很難回答,因爲缺乏示例代碼,因此不能理解你的意圖或你如何/做什麼。