Hi Friends
I want to know how to use ajax in PHP ? Please tellme anyone with an example of ajax in PHP
Thanks.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 2, 2012, 2:21 PM
Pagination using ajax in Php
Table creation
-- phpMyAdmin SQL Dump
-- version 2.11.4
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 22, 2008 at 03:06 AM
-- Server version: 5.0.51
-- PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `home_work`
--
-- --------------------------------------------------------
--
-- Table structure for table `user_info`
--
CREATE TABLE `user_info` (
`id_user_info` int(11) NOT NULL auto_increment,
`fullname` varchar(255) NOT NULL default '',
`email` varchar(255) NOT NULL default '',
`contact_no` varchar(255) NOT NULL default '',
PRIMARY KEY (`id_user_info`)
) TYPE=MyISAM AUTO_INCREMENT=24 ;
#
# Dumping data for table `user_info`
#
INSERT INTO `user_info` VALUES (1, 'John Smith', '[email protected]', '98675410181');
INSERT INTO `user_info` VALUES (2, 'Micheal Doughlas', '[email protected]', '98675410131');
INSERT INTO `user_info` VALUES (3, 'Mary Anderson', '[email protected]', '91675433181');
INSERT INTO `user_info` VALUES (4, 'James McCarthy', '[email protected]', '98675550131');
INSERT INTO `user_info` VALUES (5, 'Rosalind Holmes', '[email protected]', '94675433181');
INSERT INTO `user_info` VALUES (6, 'Pete McCuin', '[email protected]', '98475550131');
INSERT INTO `user_info` VALUES (7, 'Nicolas Finh', '[email protected]', '98675410181');
INSERT INTO `user_info` VALUES (8, 'Tommy Lee', '[email protected]', '98675710131');
INSERT INTO `user_info` VALUES (9, 'Sujoku Jim', '[email protected]', '91675433181');
INSERT INTO `user_info` VALUES (10, 'James Hull', '[email protected]', '98655550131');
INSERT INTO `user_info` VALUES (11, 'Sherly Peters', '[email protected]', '94675433181');
INSERT INTO `user_info` VALUES (12, 'Abraham Holder', '[email protected]', '98400550131');
INSERT INTO `user_info` VALUES (13, 'Pete Sampras', '[email protected]', '98600010132');
INSERT INTO `user_info` VALUES (14, 'Kirk Doughlas', '[email protected]', '98675410144');
INSERT INTO `user_info` VALUES (15, 'Dorothy James', '[email protected]', '91675433165');
INSERT INTO `user_info` VALUES (16, 'Sebastian Urchin', '[email protected]', '98675550197');
INSERT INTO `user_info` VALUES (17, 'Tabitha Gregory Golmes', '[email protected]', '94675433133');
INSERT INTO `user_info` VALUES (18, 'Albert Einstien', '[email protected]', '98475550144');
INSERT INTO `user_info` VALUES (19, 'Yasser Arafat', '[email protected]', '98675410155');
INSERT INTO `user_info` VALUES (20, 'Bruce Lee', '[email protected]', '98675710166');
INSERT INTO `user_info` VALUES (21, 'Johny Depp', '[email protected]', '91675433171');
INSERT INTO `user_info` VALUES (22, 'Celina Jaitly', '[email protected]', '98655550199');
INSERT INTO `user_info` VALUES (23, 'Leander Peas', '[email protected]', '94675433100');
config.php
$DBHOST = "localhost";
$DBNAME = "home_work";
$DBUSER = "root";
$sLink = mysql_connect($DBHOST,$DBUSER,'') or die('Connection with MySql Server failed');
mysql_select_db($DBNAME, $sLink) or die('MySql DB was not found');
?>
Pagination.php
include('./config.php');
?>
.blackText{
font-weight:normal;
font-size:11.5px;
font-family: verdana, arial, helvetica, sans-serif;
color:#000000;
font-style:normal;
}
.blackTextDDM{
font-weight:normal;
font-size:11.5px;
font-family: verdana, arial, helvetica, sans-serif;
color:#000000;
font-style:normal;
padding: 2px;
border: 1px solid #000000;
}
.blueText{
font-weight:normal;
font-size:11px;
font-style:normal;
font-family: verdana, arial, helvetica, sans-serif;
color:#84C0FC;
}
.errText{
font-weight:italic;
font-size:10.5px;
font-family: verdana, arial, helvetica, sans-serif;
color:red;
}
records.php
$sPageNo = 1;
$sRecordsPerPage = 5;
$sOrderBy = "id_user_info ASC";
if ($_POST['page_no'] || $_POST['orderby'] || $_POST['records_per_page'])
{
include('./config.php');
if($_POST['page_no'])
$sPageNo = $_POST['page_no'];
if($_POST['orderby'])
$sOrderBy = $_POST['orderby'];
if($_POST['records_per_page'])
$sRecordsPerPage = $_POST['records_per_page'];
}
$sSelQry = "SELECT count(*) as no_records FROM user_info";
$aCountRec = mysql_fetch_array(mysql_query($sSelQry));
$iTotalRecords = $aCountRec['no_records'];
$sNoOfPages = ceil($iTotalRecords/$sRecordsPerPage);
$sStartRange = ($sPageNo * $sRecordsPerPage) - $sRecordsPerPage;
$sEndRange = $sRecordsPerPage;
$sSelQry = "SELECT * FROM user_info ORDER BY $sOrderBy LIMIT $sStartRange,$sEndRange";
$aUserInfo = mysql_query($sSelQry);
if(mysql_num_rows($aUserInfo) == 0 && $_POST['records_per_page'])
{
$sPageNo = $sNoOfPages;
$sStartRange = ($sPageNo * $sRecordsPerPage) - $sRecordsPerPage;
$sEndRange = $sRecordsPerPage;
$sSelQry = "SELECT * FROM user_info ORDER BY $sOrderBy LIMIT $sStartRange,$sEndRange";
$aUserInfo = mysql_query($sSelQry);
}
//===============================
if ($sOrderBy == 'fullname ASC')
$sOrderByFN = 'fullname DESC';
else
$sOrderByFN = 'fullname ASC';
//===============================
//===============================
if ($sOrderBy == 'email ASC')
$sOrderByEM = 'email DESC';
else
$sOrderByEM = 'email ASC';
//===============================
//===============================
if ($sOrderBy == 'contact_no ASC')
$sOrderByCN = 'contact_no DESC';
else
$sOrderByCN = 'contact_no ASC';
//===============================
?>
No of Records Per Page:
".$j." "; } else { echo($j." "); } ?>
$i = ($sRecordsPerPage * $sPageNo) - ($sRecordsPerPage - 1);
while($aUserInfoRS = mysql_fetch_array($aUserInfo)) {
$sUserName = $aUserInfoRS['fullname'];
$sEmail = $aUserInfoRS['email'];
$sContact = $aUserInfoRS['contact_no'];
?>
$i++;
}
?>
ajax_sort.js
function ajax_sort(sort_order,page_no,records_per_page)
{
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
//http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}
var url = 'records.php';
var parameters = 'orderby='+sort_order+'&page_no='+page_no+'&records_per_page='+records_per_page;
http_request.onreadystatechange = ajax_sort_callback;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_request.setRequestHeader("Content-length", parameters.length);
http_request.setRequestHeader("Connection", "close");
http_request.send(parameters);
}
function ajax_sort_callback()
{
if (http_request.readyState == 4) {
if (http_request.status == 200) {
result = http_request.responseText;
//alert(result);
//return false;
document.getElementById('sorting_rec').innerHTML = result;
} else {
alert('There was a problem with the request.');
}
}
}
Thanks