How to keep the POST values in pagination when going to the next page?

G’day to all.

I am currently working on a pagination script. The script in general works when I call a query from a database that doesn’t require any input from a user.

Now I am trying to implement this pagination with a search functionality that allows a user to input some WHERE conditions within the query. The problem I am having is that the first page calls the correct details and everything appears to be fine, but then when I click on the next page, it seems as if the POST values are not carrying.

Could someone please assist?

Here is the current code:


<?php

session_start();

include('../databaseStuff.php');

$start = $_GET['start'];

if(!($start > 0)) {

	$start = 0;

}

$eu = ($start - 0);

$limit = 10;  // No of records to be shown per page.

$this1 = $eu + $limit;

$back = $eu - $limit;

$next = $eu + $limit;


if (isset($_GET['jobCategoryId'])) {

	$jobCat = $_GET['jobCategoryId'];

	$query = "SELECT jobId, jobTitle, jobSummary, jobPay, DATE_FORMAT(jobAdStart, '%D %b, %Y') as closeDate, employmentStatus, jobCity, jobState, jobPostcode, jobClassId FROM jobDetails WHERE jobCategoryId = $jobCat AND status = 'Active' LIMIT $eu, $limit";

}

if (isset($_POST['quickSearch'])) {

	$keyword = $_POST['keyword'];
	
	# $terms is what they search for, i.e how to photoshop
	# Explode the keywords
	$keywords = explode(",", $keyword);
	# Run through each and write it as SQL
	$state = $_POST['state'];
	$jobCatId = $_POST['jobCategory'];
	$jobClassId = $_POST['jobClassId'];

	$query = "SELECT jobId, jobTitle, jobSummary, jobPay, DATE_FORMAT(jobAdStart, '%D %b, %Y') as closeDate, employmentStatus, jobCity, jobState, jobPostcode, jobClassId FROM jobDetails WHERE 1 = 1 ";
	
	if (!empty($keyword)) {
		$query .= " AND (";
		foreach($keywords as $word) {
			$parts[] = " jobTitle LIKE '%" . $word . "%'";
		}
		$query .= implode(" OR ", $parts);
		$query .= ") ";
	}
	
	if (!empty($state)) {
		$query .= " AND jobState = '$state' ";
	}
	
	if (!empty($jobCatId)) {
		$query .= " AND jobCategoryId = $jobCatId ";
	}
	
	if (!empty($jobClassId)) {
		$query .= " AND jobClassId = $jobClassId ";
	}
	
	// for pagination
	$query2 = $query;
	$query2 .= "AND status = 'Active' ORDER BY jobId";
	
	$query .= " AND status = 'Active' ORDER BY jobId LIMIT $eu, $limit";	
	
}


// get the results
$result = mysql_query($query);

// for pagination
$result2 = mysql_query($query2);
$nume = mysql_num_rows($result2);

if ($nume > 0) {

	$amount = "<div id='err2' style='width: 98%'><p>You have $nume jobs matching that search criteria</p></div>";

} else {

	$amount = "<div id='err' style='width: 98%'><p>There are no jobs listed matching that search criteria. Please try again.</p></div>";
	
}



?>	

<td class="stretch_box">

    <table>


    <tr>

   	<td>

	<div style="margin-left: 15px;">

	<?php
		
		if ($back >= 0) {
			echo "<a href='?start=$back'><img style='border: none;' src='http://www.mysite.com/images/back.jpg' alt='Previous' /></a>";
		}
		
		$p=0;

		$l=1;

		for ($p=0; $p < $nume; $p=$p+$limit) {

			if ($p <> $eu) {
				echo " <a class='normal' href='?start=$p'>$l</a> | ";
			} else {
				echo " <span class='currentPage'>$l |</span>";
			}
			$l=$l+1;
		}
		
		if ($this1 < $nume) {
			echo "<a href='?start=$next'><img style='border: none;' src='http://www.mysite.com/images/forward.jpg' alt='Next' /></a>";
		}


		while ($row = mysql_fetch_array($result)) {

			$bgColor = "#ffffe8";

			$bgImage = "image.png";

			$link = "http://www.mysite.com";
			

			echo "<div style='width: 600px; background-color:".$bgColor."; background-image: URL(".$bgImage."); border: 1px solid #036; margin: 5px; padding: 5px;'>";

			echo "<div style='width: 250px; height: 100%; padding-right: 15px; float: left;'>";

			echo "<h4><a href=".$link."/jobdetails.php?jobId=".$row['jobId'].">".$row['jobTitle']."</a></h4>";

			echo "<h6><span style='color: #000;'>Job Summary:</span><br />".$row['jobSummary']."</h6>";

			echo "</div>";

			echo "<div style='float: left; margin-top: 35px;'>";

			echo "<h6><span style='color: #000;'>Salary:</span> $".$row['jobPay'].".00&nbsp;&nbsp;&nbsp;&nbsp;<span style='color: #000;'>Work Status:</span> ".$row['employmentStatus']."</h6>";

			echo "<h6><span style='color: #000;'>Location:</span> ".$row['jobCity']." | ".$row['jobState']." | ".$row['jobPostcode']."</h6>";

			echo "<h6><span style='color: #000;'>Closing Date:</span> ".$row['closeDate']."</h6>";

			echo "</div>";

			echo "<p style='float: right;'><a href=".$link."/jobdetails.php?jobId=".$row['jobId'].">View Job...</a></p>";

			echo "</div>";

		}

	?>

	</div>

    </td>

    </tr>

    </table>

</td><!-- end class stretch_box -->


You’re going to have to use either $_GET or $_SESSION for this, because, as you state correctly, $_POST data is not carried along pages.

I didn’t look at all the code, but from what you wrote, im thinking maybe you could use sessions, to store the search info.

Hope this helps

Many thanks for the very speedy reply. I thought as much however, I just thought there may have been some great trick out there that I hadn’t discovered.

Thanks again.

Using GET is most appropriate here.
Problem with using sessions for this is if your user decides to open a second browser window and enter different search criteria…it may confuse them. Although you could store the bulk in the session, and store just some kind of unique id in the url, and use that id to access the correct collection in the session.

you could make a hidden form and dump the variables into the hidden fields and submit it each time you got to and fro between pages…

GET generally should be used for any search type systems as you dont get that annoying “page refresh… repost data” notice if you go back a page.

GET generally should be used for any search type systems as you dont get that annoying “page refresh… repost data” notice if you go back a page.

I definately agree with that, that notice is annoying.

If there are no longer values on posted form and there are only few posted variables/values then i would pass them through the URL (as query string) and use GET to pass the values.

If there are lots of values posted then better to go using again form and hidden fields as spikeZ suggested.

Thanks to all suggestions.

I have just awoken from last night and the last thing I did before I went to bed was created a SESSION like this: Would appreciate any feedback about the process. (I have done some quick tests and it appears to work)


session_start();

include('../databaseStuff.php');

$start = $_GET['start'];

if(!($start > 0)) {

	$start = 0;

}

$eu = ($start - 0);

$limit = 10;  // No of records to be shown per page.

$this1 = $eu + $limit;

$back = $eu - $limit;

$next = $eu + $limit;


if (isset($_GET['jobCategoryId'])) {

	$jobCat = $_GET['jobCategoryId'];

	$query = "SELECT jobId, jobTitle, jobSummary, jobPay, DATE_FORMAT(jobAdStart, '%D %b, %Y') as closeDate, employmentStatus, jobCity, jobState, jobPostcode, jobClassId FROM jobDetails WHERE jobCategoryId = $jobCat AND status = 'Active' LIMIT $eu, $limit";

}

if (isset($_POST['quickSearch'])) {

	$keyword = $_POST['keyword'];
	
	# $terms is what they search for, i.e how to photoshop
	# Explode the keywords
	$keywords = explode(",", $keyword);
	# Run through each and write it as SQL
	$state = $_POST['state'];
	$jobCatId = $_POST['jobCategory'];
	$jobClassId = $_POST['jobClassId'];

	$query = "SELECT jobId, jobTitle, jobSummary, jobPay, DATE_FORMAT(jobAdStart, '%D %b, %Y') as closeDate, employmentStatus, jobCity, jobState, jobPostcode, jobClassId FROM jobDetails WHERE 1 = 1 ";
	
	if (!empty($keyword)) {
		$query .= " AND (";
		foreach($keywords as $word) {
			$parts[] = " jobTitle LIKE '%" . $word . "%'";
		}
		$query .= implode(" OR ", $parts);
		$query .= ") ";
	}
	
	if (!empty($state)) {
		$query .= " AND jobState = '$state' ";
	}
	
	if (!empty($jobCatId)) {
		$query .= " AND jobCategoryId = $jobCatId ";
	}
	
	if (!empty($jobClassId)) {
		$query .= " AND jobClassId = $jobClassId ";
	}
	
	// for pagination
	$query2 = $query;
	$query2 .= "AND status = 'Active' ORDER BY jobId";
	$_SESSION['query2'] = $query2; // I changed this
	
	$query .= " AND status = 'Active' ORDER BY jobId ";	
	$_SESSION['query'] = $query; // changed this
	
}
###
// placed within page to make the page limit dynamic
$query = $_SESSION['query'];
$query .= " LIMIT $eu, $limit ";
// get the results
$result = mysql_query($query);

// for pagination
$result2 = mysql_query($_SESSION['query2']);
$nume = mysql_num_rows($result2);

if ($nume > 0) {

	$amount = "<div id='err2' style='width: 98%'><p>You have $nume jobs matching that search criteria</p></div>";

} else {

	$amount = "<div id='err' style='width: 98%'><p>There are no jobs listed matching that search criteria. Please try again.</p></div>";
	
}



?>	

<td class="stretch_box">
<!-- and then rest of code -->


Your process looks pretty sound :slight_smile:
The only suggestion that I have, is to check if $_SESSION[‘query’] and $_SESSION[‘query2’] exist before using them in the SQL queries. Reason being, that someone might land on the page without having come from the page where they enter the search criteria (spelling…), and thus they would get errors. You could do something like this


  if(array_key_exists('query' , $_SESSION)) {
    // do query etc.
  } else {
    echo 'Please enter some search criteria.';
  }

Open a second window in the same browser.
Use each window to search via different criteria.
Go back and forth between the 2 windows, using each.

Be sure you’re ok with that, as it will surely make users think your search results are broken.

Keeping it all in the url is better.