我通过ajax到search.html调用search.php页面。问题是,因为我已经实现了分页,当用户点击“下一步”按钮时,带有来自search.html的搜索关键字的文本框'消失'[因为该页面转到没有文本框元素的search.php]
当用户通过分页查看记录时,我希望带有搜索关键字的文本框在那里。我怎么做到这一点?
search.html:
<body>
<form name="myform" class="wrapper">
<input type="text" name="q" onkeyup="showPage();" class="txt_search"/>
<input type="button" name="button" onclick="showPage();" class="button"/>
<p>
<div id="txtHint"></div>
<div id="page"></div>
</form>
</body>
search.php [相关部分]:
$self = $_SERVER['PHP_SELF'];
$limit = 5; //Number of results per page
$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="caption">Search Results</div>
<div class="center_div">
<table>
<?php while ($row= mysql_fetch_array($result, MYSQL_ASSOC)) {
$cQuote = highlightWords(htmlspecialchars($row['cQuotes']), $search_result);
?>
<tr>
<td style="text-align:right; font-size:15px;"><?php h($row['cArabic']); ?></td>
<td style="font-size:16px;"><?php echo $cQuote; ?></td>
<td style="font-size:12px;"><?php h($row['vAuthor']); ?></td>
<td style="font-size:12px; font-style:italic; text-align:right;"><?php h($row['vReference']); ?></td>
</tr>
<?php } ?>
</table>
</div>
<?php
//Create and print the Navigation bar
$nav="";
if($page > 1) {
$nav .= "<a href=\"$self?page=" . ($page-1) . "&q=" .urlencode($search_result) . "\">< Prev</a>";
$first = "<a href=\"$self?page=1&q=" .urlencode($search_result) . "\"><< First</a>" ;
}
else {
$nav .= " ";
$first = " ";
}
for($i = 1 ; $i <= $numpages ; $i++) {
if($i == $page) {
$nav .= "<B>$i</B>";
}else{
$nav .= "<a href=\"$self?page=" . $i . "&q=" .urlencode($search_result) . "\">$i</a>";
}
}
if($page < $numpages) {
$nav .= "<a href=\"$self?page=" . ($page+1) . "&q=" .urlencode($search_result) . "\">Next ></a>";
$last = "<a href=\"$self?page=$numpages&q=" .urlencode($search_result) . "\">Last >></a> ";
}
else {
$nav .= " ";
$last = " ";
}
echo "<br /><br />" . $first . $nav . $last;
}
ajax.js:
var xmlHttp
function showPage(str)
{
var str = document.myform.q.value;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="search.php";
url += "?q="+str+"&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
function GetXmlHttpObject() {
var xmlHttp=null;
try {
// For these browsers: Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}catch (e){
//For Internet Explorer
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
} //end function */
答案 0 :(得分:1)
您的下一个和上一个链接也需要使用AJAX。
我提供了一个代码示例,但是您没有提供用于通过AJAX获取search.php的代码。如果您发布您的AJAX代码,我可以演示您需要通过AJAX获取下一页和上一页的更改。
编辑:我会修改showPage
功能以接受页码,并使下一个和上一个链接调用:
function showPage(str,page)
{
var str = document.myform.q.value;
if(typeof page == 'undefined')
page = 0;
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="search.php";
url += "?q="+str+"&page="+page+"&list=";
url += "&sid="+Math.random();
...
然后,您的网页链接应如下所示:
$nav .= "<a onclick=\"showPage('',$i); return false;\" href=\"$self?page=$i&q=" .urlencode($search_result) . "\">$i</a>";