自己總結了些屁經驗
1.用ajax post數據到後台頁面後,接著要重新連接數據庫,別以為用之前的session連接過就可以了
2.為了處理返回亂碼的問題,我添加了header("Content-Type:text/html;charset=GB2312");就可以正常顯示了,後來在firefox下檢驗,卻提示我下載這個網頁,上網搜了不少資料,得到一個模糊的認識就是網頁代碼有語法錯誤,firefox為了安全起見不會直接顯示而是提示下載,我重新檢查了剛才那條語句,發現自己多寫了個“\”,把它去掉後問題就解決了,哈哈,所以遇到這樣的問題,好好檢查一下html tag吧,畢竟firefox可不像ie那樣smart
3.最後說一句,做web site的開發者,要負責任,別以為在ie下測試通過就萬事大吉,畢竟不是所有人都用ie,還得要在別的浏覽器下多做測試,這樣才顯示出你的專業水准
ajax腳本:
復制代碼 代碼如下:
<script>
function viewpage(p){
if(window.XMLHttpRequest){
var xmlReq = new XMLHttpRequest();
} else if(window.ActiveXObject) {
var xmlReq = new ActiveXObject('Microsoft.XMLHTTP');
}
var formData = "page="+p;
xmlReq.onreadystatechange = function(){
if(xmlReq.readyState == 4){
document.getElementById('content2').innerHTML = xmlReq.responseText;
}
}
xmlReq.open("post", "hotel_list.php", true);
xmlReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlReq.send(formData);
return false;
}
</script>
調用:
復制代碼 代碼如下:
header("Content-Type:text/html;charset=GB2312");
$pagesize=10;
//echo $_POST['page'];
$result = mysql_query("Select count(DISTINCT hotelname) FROM ".TBL_HOTELS);
$myrow = mysql_fetch_array($result);
$numrows=$myrow[0];
$pages=intval($numrows/$pagesize);
if ($numrows%$pagesize)
$pages++;
if (isset($_POST['page'])){
$page=intval($_POST['page']);
}
else{
//設置為第一頁
$page=1;
}
$first=1;
$prev=$page-1;
$next=$page+1;
$last=$pages;
//計算記錄偏移量
$offset=$pagesize*($page - 1);
//讀取指定記錄數
$result=mysql_query("select `hotelname` , count( * ) from ".TBL_HOTELS." GROUP BY `hotelname` order by id desc limit $offset,$pagesize");
$num = mysql_num_rows($result);
while ($row = mysql_fetch_array($result,MYSQL_NUM)) {
$hotelname[] = $row[0];
$countpeople[] = $row[1];
}
for($a=0;$a<$num;$a++)
{
//$result=mysql_query("select count(title) from " . TBL_Comments ." where `title`=\"".$title[$a]."\"");
//$row = mysql_fetch_row($result);
echo "<TABLE style=\"MARGIN-BOTTOM: 20px\" cellSpacing=0 cellPadding=0 width=100% border=0>\n";
echo "<TBODY>\n";
echo "<TR>\n";
echo "<TD style=\"PADDING-TOP: 5px\" vAlign=top align=left width=80>\n";
//rating_bar($title[$a],5);
echo "</TD>\n";
echo "<TD style=\"PADDING-TOP: 5px\" align=left width=100%><A title=$hotelname[$a] style=\"FONT-SIZE: 14px\" href=#>$hotelname[$a]</A>\n";
echo "</TD></TR>\n";
echo " <TR>\n";
echo "<TD></TD>\n";
echo "<TD style=\"PADDING-LEFT: 0px\">\n";
echo "<IMG src=\"images/comment.gif\" border=0> 推薦人數:($countpeople[$a]) |\n";
echo "<SPAN>平均分:<STRONG></STRONG> (".$count."票) | 評論數:()</SPAN>\n";
echo "</TD></TR></TBODY></TABLE>\n";
}
echo "<TABLE style=\"MARGIN-TOP: 30px\" cellSpacing=0 cellPadding=0 width=\"100%\"";
echo "border=0>";
echo "<TBODY><TR><TD colSpan=3 height=20>";
echo "<DIV align=center>";
echo "<P align=left><FONT color=red>第".$page."頁/總".$pages."頁 | 總".$numrows."條</FONT> | ";
if ($page>1) echo "<a onclick=\"viewpage(".$first.")\" href='#'>首頁</a> | ";
if ($page>1) echo "<a onclick=\"viewpage(".$prev.")\" href='#'>上頁</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$next.")\" href='#'>下頁</a> | ";
if ($page<$pages) echo "<a onclick=\"viewpage(".$last.")\" href='#'>尾頁</a>";
echo "轉到第 <INPUT maxLength=3 size=3 value=1 name=goto_page> 頁 <INPUT hideFocus onclick=\"viewpage(document.all.goto_page.value)\" type=button value=Go name=cmd_goto>";
echo "</P></DIV></TD></TR></TBODY></TABLE>";