Jump to content

Issues with cookie on page reload


Recommended Posts

Hello PHP Freaks,

 

I've made a form that generates a list of entries from a database by constructing a query based on the options selected. I have an if statement that constructs the query if a $_POST variable is set.

 

I'm also using a pagination script. So whenever I navigate between the pagination links I lose my query because the page is reloading and the condition needed to construct the query is no longer true.

 

So to retain the query between the pagination links I attempted to put the original query constructed in a cookie and if another search is made the cookie is unset within the if statement.

 

The issues having with this are the cookie appears to not be set on the first submission but does on the second. Also the cookie disappears when navigating between the pagination links.

 

Take a look at the code I've written.

 

Here's the code for my form:

 

<form action="posts.php" method="post">

<?php

$sector_query = mysql_query('SELECT DISTINCT sector FROM posting'); 
$location_query = mysql_query('SELECT DISTINCT location FROM posting'); 

?>

<h1 class="dark_grey">Search</h1>

<p class="dark_grey">Sector: <br>

</p>
<p><select name="sector" class="drop_menu">
	<option value="000" selected="selected">Select</option>

<?php
	while ($option = mysql_fetch_array($sector_query)) {

		echo "<option>{$option['sector']}</option>";
	}
?>

</select></p>


<p class="dark_grey">Location: <br>

</p>
<p> <select name="location" class="drop_menu">
	<option value="000" selected="selected">Select</option>
<?php
	while ($option = mysql_fetch_array($location_query)) {

		echo "<option>{$option['location']}</option>";
	}
?>


</select></p>

<p class="dark_grey">Hours:</p>
<p class="dark_grey" ><select name="hours" class="drop_menu">
	<option value="000" selected="selected">All</option>

		<option>Full time</option>
		<option>Part time</option>


</select></p></p>

<p class="button_position"><input type="submit" name="search" value="Search" class="button" /></p>
</form>

 

Here's the code for the if statement that generates the query and sets/unsets the cookie:

 

<?php

	if (isset($_POST[])) {

	$find = array();
	$area = array();

	if (isset($_POST ['location']) && !($_POST ['location'] == "000")) {

		$find [] = $_POST ['location']; 		
		$area [] = "location";
	}


	if (isset($_POST ['sector']) && !($_POST ['sector'] == "000")) {

		$find [] = $_POST ['sector']; 
		$area [] = "sector";
	}	

	if (isset($_POST ['hours']) && !($_POST ['hours'] == "000")) {

		$find [] = $_POST ['hours']; 	
		$area [] = "hours";
	}	

	while ((list($key1, $val1) = each($find)) && (list($key2, $val2) = each($area))) {
		if ($key1 == 0) {
			$result =  " WHERE " .  strtolower($val2)  . "= " .  "'" . strtolower($val1) .  "'" . " " ;  	
		}
		if ($key1 >= 1) {
			$result .=  "AND " . strtolower($val2) . "= " .  "'" . strtolower($val1) .  "'" . " " ;  	
		}

	}

	setcookie("testcookie", "$result", time()-36000);
	setcookie("testcookie", $result);
	$ret_result = str_replace('\\', '', $_COOKIE["testcookie"]);

}	

?>

 

I then put the cookie that contains the query in a variable.

 

Let me know if you need to see any other pieces of my code.

 

Thanks

 

Kevin

 

Link to comment
https://forums.phpfreaks.com/topic/240132-issues-with-cookie-on-page-reload/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.