萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP+Ajax實現分頁技術

PHP+Ajax實現分頁技術

PHP+Ajax實現分頁技術:

基於php和ajax的分頁技術代碼,下面有兩個php文件,一個是sn_inq.php,另一個是sn_show.php,前一個php文件調用後一個php文件,實現ajax分頁,運行sn_inq即可實現效果,不過得修改數據庫哦。具體代碼如下:紅色標注的地方要特別注意修改哦!

我的數據庫名是inv,表名是sn,字段有:sn_id,sn_plant,sn_sales,sn_act,sn_type,sn_sts.....

1.sn_inq.php

//getFormValue 用於獲取表單中所有輸入控件的值,並將輸入值組成一個字符串傳到服務器。

<script type="text/javascript"> 
function getFormValue(form){
var str='';
var ft,fv;
for (var i=0; i<form.elements.length; i++){
fv = form.elements[i];
ft = fv.type.toLowerCase();
switch(ft){
case 'select-one':
str+=fv.name+'='+escape(fv.value)+'&';
break;
case 'radio':
if (fv.checked){
str+=fv.name+'='+escape(fv.value)+'&';
}
break;
case 'checkbox':
if (fv.checked){
str+=fv.name+'='+escape(fv.value)+'&';
}
break;
case 'text':
str+=fv.name+'='+escape(fv.value)+'&';
break;
case 'password':
str+=fv.name+'='+escape(fv.value)+'&';
break;
case 'hidden':
str+=fv.name+'='+escape(fv.value)+'&';
break;
case 'textarea':
str+=fv.name+'='+escape(fv.value)+'&';
break;
default:
break;
}
}
return str.split(/s/).join('')
}
 

//showcomment(page) 用於顯示分頁數據,被查詢按鈕onclick 事件調用, url為後台處理數據並輸出XML格式數據的文件url。


function showcomment(page) {
var x = new Ajax('statusid', 'XML');
url = 'sn_show.php?page='+page+'&'+getFormValue(document.form1);

x.get(url , function(s) {
if(s.lastChild){
getbyid("show").innerHTML = "Loading……";
getbyid("show").innerHTML = s.lastChild.firstChild.nodeValue;
removeLoading(document.getElementById("show"));
}
else{
document.form1.submit();
}
});
}

//displayLoading用於顯示Loading,提示用戶等待

function displayLoading(element) {
var image = document.createElement("img");
image.setAttribute("src","progressbar.gif");
image.setAttribute("title","loading...");
var text = document.createTextNode("loading……");
element.appendChild(image);
element.appendChild(text);
}

//removeLoading用於去掉Loading

function removeLoading(element){
var image = element.getElementsByTagName("img");
for(var i=0;i<image.length;i++){
element.removeChild(image[i]);
}
}

//ajax

function Ajax(statusId, recvType) {
var aj = new Object();
displayLoading(document.getElementById("show"));
var clientHeight = scrollTop = 0;
if(navigator.userAgent.toLowerCase().indexOf('opera') > -1) {
clientHeight = document.body.clientHeight;
scrollTop = document.body.scrollTop;
} else {
clientHeight = document.documentElement.clientHeight;
scrollTop = document.documentElement.scrollTop;
}
if(document.getElementById(statusId)) {
aj.statusId = document.getElementById(statusId);
document.getElementById(statusId).style.top = 10+"px";
} else {
var divElement = document.createElement("DIV");
divElement.id = "xspace-tipDiv";
divElement.className = "xspace-ajaxdiv";
divElement.style.cssText = "width:200px; height:40px; line-height: 40px; text-align: center;";
divElement.style.left = 10+"px";
divElement.style.top = 10+"px";//(clientHeight +scrollTop - 60)
divElement.id = statusId;
document.body.appendChild(divElement);
aj.statusId = divElement;
}
aj.targetUrl = '';
aj.sendString = '';
aj.recvType = recvType ? recvType : 'HTML';//HTML XML
aj.resultHandle = null;
aj.createXMLHttpRequest = function() {
var request = false;
if(window.XMLHttpRequest) {
request = new XMLHttpRequest();
if(request.overrideMimeType) {
request.overrideMimeType('text/xml');
}
} else if(window.ActiveXObject) {
var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
for(var i=0; i<versions.length; i++) {
try {
request = new ActiveXObject(versions[i]);
if(request) {
return request;
}
} catch(e) {
//alert(e.message);
}
}
}
return request;
}
aj.XMLHttpRequest = aj.createXMLHttpRequest();
aj.processHandle = function() {
aj.statusId.style.display = '';
if(aj.XMLHttpRequest.readyState == 4) {
if(aj.XMLHttpRequest.status == 200) {
if(aj.recvType == 'HTML') {
aj.resultHandle(aj.XMLHttpRequest.responseText);
} else if(aj.recvType == 'XML') {
aj.resultHandle(aj.XMLHttpRequest.responseXML);
}
aj.statusId.style.display = 'none';
} else {
aj.statusId.innerHTML = xml_http_load_failed;
}
}
}

aj.get = function(targetUrl, resultHandle) {
aj.targetUrl = targetUrl;
aj.XMLHttpRequest.onreadystatechange = aj.processHandle;
aj.resultHandle = resultHandle;
if(window.XMLHttpRequest) {
aj.XMLHttpRequest.open('GET', aj.targetUrl);
aj.XMLHttpRequest.send(null);
} else {
aj.XMLHttpRequest.open("GET", targetUrl, true);
aj.XMLHttpRequest.send();
}
}

aj.post = function(targetUrl, sendString, resultHandle) {

copyright © 萬盛學電腦網 all rights reserved