Kevin_Constantine Posted June 22, 2011 Share Posted June 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240132-issues-with-cookie-on-page-reload/ Share on other sites More sharing options...
Pikachu2000 Posted June 22, 2011 Share Posted June 22, 2011 Cookie data is not available until the next page load. See the "Description" block here: setcookie Quote Link to comment https://forums.phpfreaks.com/topic/240132-issues-with-cookie-on-page-reload/#findComment-1233435 Share on other sites More sharing options...
Kevin_Constantine Posted June 22, 2011 Author Share Posted June 22, 2011 Hi thanks for the quick reply. Can you tell me the best way to retain the form data? Quote Link to comment https://forums.phpfreaks.com/topic/240132-issues-with-cookie-on-page-reload/#findComment-1233437 Share on other sites More sharing options...
TeNDoLLA Posted June 22, 2011 Share Posted June 22, 2011 Add the search query parameter to the url in the links for the pagination if the search has been made. Quote Link to comment https://forums.phpfreaks.com/topic/240132-issues-with-cookie-on-page-reload/#findComment-1233447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.