2
我有一個搜索表單,用戶輸入關鍵字並將結果與分頁一起顯示。一切工作正常,除了當用戶點擊'下一步'按鈕時,頁面加載通過AJAX檢索數據時,分頁面板也會消失。分頁面板應保持靜態
如何在檢索數據時使分頁面板處於靜態狀態?
search.html:
<form name="myform" class="wrapper">
<input type="text" name="q" id="q" onkeyup="showPage();" class="txt_search"/>
<input type="button" name="button" onclick="showPage();" class="button"/>
<p> </p>
<div id="txtHint"></div>
</form>
AJAX:
var url="search.php";
url += "?q="+str+"&page="+page+"&list=";
url += "&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
function stateChanged(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
} //end if
} //end function
的search.php:
$self = $_SERVER['PHP_SELF'];
$limit = 3; //Number of results per page
$adjacents = 2;
$numpages=ceil($totalrows/$limit);
$query = $query." ORDER BY idQuotes LIMIT " . ($page-1)*$limit . ",$limit";
$result = mysql_query($query, $conn)
or die('Error:' .mysql_error());
?>
<div class="search_caption">Search Results</div>
<div class="search_div">
<table class="result">
<?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
?>
<tr>
. . .display results. . .
</tr>
<?php } ?>
</table>
</div>
<hr>
<div class="searchmain">
<?php
//Create and print the Navigation bar
$nav="";
$next = $page+1;
$prev = $page-1;
if($page > 1) {
$nav .= "<a onclick=\"showPage('','$prev'); return false;\" href=\"$self?page=" . $prev . "&q=" .urlencode($search_result) . "\">< Prev</a>";
$first = "<a onclick=\"showPage('','1'); return false;\" href=\"$self?page=1&q=" .urlencode($search_result) . "\"> << </a>" ;
}
else {
$nav .= " ";
$first = " ";
}
for($i = 1 ; $i <= $numpages ; $i++) {
if($i == $page) {
$nav .= "<span class=\"no_link\">$i</span>";
}else{
$nav .= "<a onclick=\"showPage('',$i); return false;\" href=\"$self?page=" . $i . "&q=" .urlencode($search_result) . "\">$i</a>";
}
}
if($page < $numpages) {
$nav .= "<a onclick=\"showPage('','$next'); return false;\" href=\"$self?page=" . $next . "&q=" .urlencode($search_result) . "\">Next ></a>";
$last = "<a onclick=\"showPage('','$numpages'); return false;\" href=\"$self?page=$numpages&q=" .urlencode($search_result) . "\"> >> </a>";
}
else {
$nav .= " ";
$last = " ";
}
echo $first . $nav . $last;
?>
</div>
請詳細說明。 – input 2010-05-22 21:36:28
那麼,假設你在showPage()中做了一個AJAX調用,那麼在收集數據之後,你可以用JS改變活動頁面狀態,並且只更改表格(你可能想爲它查找id以便查找)。你可能也想使用一些AJAX庫 - 比如sajax或類似的東西來獲得一個清潔的代碼。 – Nux 2010-05-23 15:32:31