Hi akmal1981,
you have to do several changes on both HTML and php pages to get this works.
HTML Changes
<html>
<head>
<script type="text/javascript">
//Changes:: add a parameter to get the pagin working
function ajaxFuntion(p){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){ // onreadystatechange stores a function
if(ajaxRequest.readyState == 4){ // when readyState is four means response is complete and we can get our data
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var type = document.getElementById('type').value; // get value from HTML input
var location = document.getElementById('location').value;
var price = document.getElementById('price').value;
//Changes: Add the currentpage parameter to the get array and use p as its value
var queryString = "?type="+type+"&location="+location+"&price="+price+"¤tpage="+p; // query string to pass information from HTML to PHP script
ajaxRequest.open("GET","getlocationxyz.php" + querystring,true); // sending request using open method. again, the problem could be here
ajaxRequest.send(null); // send request to server
}
</script>
</head>
<body>
<!-- Changes: Remove form since its all AJAX based -->
<!-- <form method="get" action="getlocationxyz.php"> -->
<!-- Changes: Add the ID property for all the HTML elements otherwise your 'document.getElementById' function will fail -->
<select name="type" id="type">
<option value="">Select property type:</option>
<option value="ALL">All</option>
<option value="factory">factory</option>
<option value="house">house</option>
<option value="condominium">condominium</option>
</select>
<select name="location" id="location">
<option value="">Select property location:</option>
<option value="ALL">All</option>
<option value="bangkok">bangkok</option>
<option value="Patpong">Patpong</option>
<option value="Chiang Mai">Chiang Mai</option>
</select>
<select name="price" id="price" >
<option value="">Select price:</option>
<option value="ALL">All</option>
<option value="code1">THB0 - THB50</option>
<option value="code2">THB50 - THB100</option>
<option value="code3">THB50 - THB100</option>
</select>
<!-- Changes: convert submit button to normal button since we only need onclick event -->
<input type="button" Value="Submit" onclick="ajaxFuntion(1)">
<br />
<div id="ajaxDiv"><b>Property information will be listed here</b></div> </body> </html>
PHP changes
<?php
//I USED YOUR CONNECTION BUT I PREFER YOU GET FAMILIER WITH PDO
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("a_database", $con);
//LITTLE BIT OF VALIDATION IF USER ACCESS THE PAGE WITHOUT TYHE GET PARAMETERS
$gType = isset($_GET['type']) ? $_GET['type'] : 'ALL';
$gLocation = isset($_GET['location']) ? $_GET['location'] : 'ALL';
$gPrice = isset($_GET['price']) ? $_GET['price'] : 'ALL';
$gHref="ContactUs_OurTeam(2).html#";
//GOT RID OF ALL THE IF CONDITIONS. IF THE VALUES ARE NOT EQUAL TO 'ALL', USE THEM IN THE QUERY
$subQuery = array();
if($gType <> 'ALL')
$subQuery[] = 'type = \''.$gType.'\'';
if($gLocation <> 'ALL')
$subQuery[] = 'location = \''.$gLocation.'\'';
if($gPrice <> 'ALL')
$subQuery[] = 'price = \''.$gPrice.'\'';
$sql = "SELECT count(*) FROM property";
if(count($subQuery) > 0)
$sql .= ' WHERE '.implode('AND ', $subQuery);
$result = mysql_query($sql,$con) or trigger_error("SQL",E_USER_ERROR);
$r=mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 2;
$totalpages = ceil($numrows/$rowsperpage);
$currentpage = isset($_GET['currentpage']) ? $_GET['currentpage'] : 1;
$offset = ($currentpage - 1) * $rowsperpage;
$centerPages = "";
$paginationDisplay = "";
//SIMPLYFIED YOUR CODE JUST INTO ONE FOR LOOP
for($page = 1; $page <= $totalpages; $page++)
{
if ($page == $currentpage)
$centerPages .= ' <span class="pagNumActive">'.$page.'</span>';
else
$centerPages .= ' <a href="javascript:ajaxFuntion('.$page.');">'.$page.'</a>';
}
if ($totalpages != 1) {
$paginationDisplay .= 'Page <strong>' . $currentpage . '</strong> of ' . $totalpages. ' '; // instead of $pn insert $currentpage
if ($currentpage != 1) {
$previous = $currentpage - 1;
$paginationDisplay .= ' <a href="javascript:ajaxFuntion('.$previous.');"> Back</a>';
}
$paginationDisplay .= '<span class="paginationNumbers">' . $centerPages . '</span>';
if ($currentpage != $totalpages) {
$nextPage = $currentpage + 1;
$paginationDisplay .= ' <a href="javascript:ajaxFuntion('.$nextPage.');"> Next</a>';
}
}
if($numrows == 0)
echo 'No Records found';
else
{
?>
<br> current page: <?php echo $currentpage; ?> <br>
<div style="margin-left:64px; margin-right:64px;">
<h2>Total Items: <?php echo $numrows ; ?></h2>
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<div style="margin-left:64px; margin-right:64px;">
<?php
$sql = 'SELECT Type,Price1,Location FROM property';
if(count($subQuery) > 0)
$sql .= ' WHERE '.implode('AND ', $subQuery);
$sql .= ' LIMIT '.$offset.','.$rowsperpage;
$result = mysql_query($sql);
?>
$display_string = "<table border='1'>";
$display_string .= <tr>
$display_string .= <th>Type</th>
$display_string .= <th>Price</th>
$display_string .= <th>Location</th>
$display_string .=</tr>
<?php
while($row = mysql_fetch_array($result)) {
$display_string .= "<tr>";
$display_string .= "<td><a href='$gHref'>" . $row['Type'] . "</a></td>";
$display_string .= "<td>" . $row['Price1'] . "</td>";
$display_string .= "<td>" . $row['Location'] . "</td>";
$display_string .= "</tr>";
}
?>
$display_string .= "</table>";
echo $display_string;
</div>
<div style="margin-left:58px; margin-right:58px; padding:6px; background-color:#FFF; border:#999 1px solid;"><?php echo $paginationDisplay; ?></div>
<?php
}
?>
If you closely look at my php code youll see I have removed all the unnecessary html tags (<head>, <body>..etc) This file should only have the content part. Otherwise when this content added the main page (after ajax call) youll get duplicate <head>, <body> tags.
Please remember I haven`t test this code but it should work fine. please post any problems...
Please check the code and undestand it without just copy and pasting it to your code.
Thanks,
best regards,
niroshan