Ajax Search Record Paging/Pagination (PHP+MySQL & ASP+Access) |
Ajax Search Record (PHP+MySQL & ASP+Access) เป็นตัวอย่างการทำ Form Search หรือค้นหาข้อมูลด้วย Ajax และยังเพิ่มเติมในส่วนของการแบ่งการแสดงผลออกเป็นหน้า มีตัวอย่างทั้ง PHP+MySQL และ ASP+Access
PHP & MySQL
AjaxPHPSearchRecordPaging1.php
<?php
/*** By Weerachai Nukitram***/
/*** http://www.ThaiCreate.Com ***/
?>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(Search,Page) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'AjaxPHPSearchRecordPaging2.php';
var pmeters = 'mySearch='+Search;
var pmeters = "mySearch=" + Search +
"&myPage=" + Page;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.setRequestHeader("Connection", "close");
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
</script>
<body Onload="JavaScript:doCallAjax('','');">
<h1>My Customer</h1>
<form name="frmMain">
Search <input type="text" name="txtSearch" id="txtSearch">
<input type="button" name="btnSearch" id="btnSearch" value="Search" OnClick="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'1');">
<br><br>
<span id="mySpan"></span>
</form>
</body>
</html>
AjaxPHPSearchRecordPaging2.php
<?php
/*** By Weerachai Nukitram ***/
/*** http://www.ThaiCreate.Com ***/
$strSearch = $_POST["mySearch"];
$strPage = $_POST["myPage"];
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");
$strSQL = "SELECT * FROM customer WHERE Name LIKE '%".$strSearch."%' ";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
$Num_Rows = mysql_num_rows($objQuery);
$Per_Page = 2; // Per Page
$Page = $strPage;
if(!$strPage)
{
$Page=1;
}
$Prev_Page = $Page-1;
$Next_Page = $Page+1;
$Page_Start = (($Per_Page*$Page)-$Per_Page);
if($Num_Rows<=$Per_Page)
{
$Num_Pages =1;
}
else if(($Num_Rows % $Per_Page)==0)
{
$Num_Pages =($Num_Rows/$Per_Page) ;
}
else
{
$Num_Pages =($Num_Rows/$Per_Page)+1;
$Num_Pages = (int)$Num_Pages;
}
$strSQL .=" order by CustomerID ASC LIMIT $Page_Start , $Per_Page";
$objQuery = mysql_query($strSQL);
?>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID</div></th>
<th width="98"> <div align="center">Name</div></th>
<th width="198"> <div align="center">Email</div></th>
<th width="97"> <div align="center">CountryCode</div></th>
<th width="59"> <div align="center">Budget</div></th>
<th width="71"> <div align="center">Used</div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery))
{
?>
<tr>
<td><div align="center"><?php echo $objResult["CustomerID"];?></div></td>
<td><?php echo $objResult["Name"];?></td>
<td><?php echo $objResult["Email"];?></td>
<td><div align="center"><?php echo $objResult["CountryCode"];?></div></td>
<td align="right"><?php echo $objResult["Budget"];?></td>
<td align="right"><?php echo $objResult["Used"];?></td>
</tr>
<?php
}
?>
</table>
<br>
Total <?php echo $Num_Rows;?> Record : <?php echo $Num_Pages;?> Page :
<?php
if($Prev_Page)
{
echo " <a href=\"JavaScript:doCallAjax(document.getElementById('txtSearch').value,'$Prev_Page')\"><< Back</a> ";
}
for($i=1; $i<=$Num_Pages; $i++){
if($i != $Page)
{
echo "[ <a href=\"JavaScript:doCallAjax(document.getElementById('txtSearch').value,'$i')\">$i</a> ]";
}
else
{
echo "<b> $i </b>";
}
}
if($Page!=$Num_Pages)
{
echo " <a href=\"JavaScript:doCallAjax(document.getElementById('txtSearch').value,'$Next_Page')\">Next >></a> ";
}
mysql_close($objConnect);
?>
Screenshot
ASP & Access
AjaxASPSearchRecordPaging1.asp[/head]
[code]<%
'*** By Weerachai Nukitram ***'
'*** http://www.ThaiCreate.Com ***'
%>
<html>
<head>
<title>ThaiCreate.Com Ajax Tutorial</title>
</head>
<script language="JavaScript">
var HttPRequest = false;
function doCallAjax(Search,Page) {
HttPRequest = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
HttPRequest = new XMLHttpRequest();
if (HttPRequest.overrideMimeType) {
HttPRequest.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
HttPRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
HttPRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!HttPRequest) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'AjaxASPSearchRecordPaging2.asp';
var pmeters = 'mySearch='+Search;
var pmeters = "mySearch=" + Search +
"&myPage=" + Page;
HttPRequest.open('POST',url,true);
HttPRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
HttPRequest.setRequestHeader("Content-length", pmeters.length);
HttPRequest.send(pmeters);
HttPRequest.onreadystatechange = function()
{
if(HttPRequest.readyState == 3) // Loading Request
{
document.getElementById("mySpan").innerHTML = "Now is Loading...";
}
if(HttPRequest.readyState == 4) // Return Request
{
document.getElementById("mySpan").innerHTML = HttPRequest.responseText;
}
}
}
</script>
<body Onload="JavaScript:doCallAjax('','');">
<h1>My Customer</h1>
<form name="frmMain">
Search <input type="text" name="txtSearch" id="txtSearch">
<input type="button" name="btnSearch" id="btnSearch" value="Search" OnClick="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'1');">
<br><br>
<span id="mySpan"></span>
</form>
</body>
</html>
AjaxASPSearchRecordPaging2.asp
<%
'*** By Weerachai Nukitram ***'
'*** http://www.ThaiCreate.Com ***'
Option Explicit
Dim strSearch,strPage
strSearch = Request.Form("mySearch")
strPage = Request.Form("myPage")
Dim Conn,strSQL,objRec
Set Conn = Server.Createobject("ADODB.Connection")
Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/mydatabase.mdb"),"" , ""
'*** Search By Name or Email ***'
strSQL = "SELECT * FROM customer "
strSQL = strSQL & "WHERE (Name LIKE '%"& strSearch &"%' "
strSQL = strSQL & "or Email LIKE '%"& strSearch &"%' ) "
Set objRec = Server.CreateObject("ADODB.Recordset")
objRec.Open strSQL, Conn, 1,3
If objRec.EOF Then
Response.write (" Not found record.")
Else
Dim PageLen,PageNo,TotalRecord,TotalPage,No,intID
PageLen = 2
PageNo = strPage
if PageNo = "" Then PageNo = 1
TotalRecord = objRec.RecordCount
objRec.PageSize = PageLen
TotalPage = objRec.PageCount
objRec.AbsolutePage = PageNo
%>
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">CustomerID</div></th>
<th width="98"> <div align="center">Name</div></th>
<th width="198"> <div align="center">Email</div></th>
<th width="97"> <div align="center">CountryCode</div></th>
<th width="59"> <div align="center">Budget</div></th>
<th width="71"> <div align="center">Used</div></th>
</tr>
<%
No=1
Do While Not objRec.EOF and No <= PageLen
%>
<tr>
<td><div align="center"><%=objRec.Fields("CustomerID").Value%></div></td>
<td><%=objRec.Fields("Name").Value%></td>
<td><%=objRec.Fields("Email").Value%></td>
<td><div align="center"><%=objRec.Fields("CountryCode").Value%></div></td>
<td align="right"><%=objRec.Fields("Budget").Value%></td>
<td align="right"><%=objRec.Fields("Used").Value%></td>
</tr>
<%
No = No + 1
objRec.MoveNext
Loop
%>
</table>
Total : <%=TotalRecord%> Page <%=PageNo%> All Page <%=TotalPage%>
<% IF Cint(PageNo) > 1 then %>
<a href="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'1')"><< First</a>
<a href="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'<%=PageNo-1%>')">< Back</a>
<% End IF%>
<% IF Cint(PageNo) < TotalPage Then %>
<a href="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'<%=PageNo+1%>')">Next ></a>
<a href="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'<%=TotalPage%>')">Last >></a>
<% End IF%>
<br>
Go to
<% For intID = 1 To TotalPage%>
<% if intID = Cint(PageNo) Then%>
<b><%=intID%></b>
<%Else%>
<a href="JavaScript:doCallAjax(document.getElementById('txtSearch').value,'<%=intID%>')"><%=intID%></a>
<%End IF%>
<%Next%>
<%
objRec.Close()
Conn.Close()
Set objRec = Nothing
Set Conn = Nothing
End IF
%>
</body>
</html>
|