AdRock Posted March 23, 2008 Share Posted March 23, 2008 I have passed some variables from a search form to a page which diplays results I am trying to get the same variable that was passed from the form back into the same page when i refresh the page What is the best way to do this because as soon as the page calls itself, it has lost the variables Here is my test script <?php $title = "Car Shares"; include("includes/init.php"); include_once("header.inc.php"); if (!empty($_POST['address'])) $address = $_POST['address']; if (!empty($_POST['departure'])) $departure = $_POST['departure']; if ($_POST['seats'] !="any" ) $seats = $_POST['seats']; if ($seats =="any") $seats=""; $default_sort = 'id'; $allowed_order = array ('start_street', 'depart_time'); /* if order is not set, or it is not in the allowed * list, then set it to a default value. Otherwise, * set it to what was passed in. */ if (!isset ($_POST['sortby']) || !in_array ($_POST['sortby'], $allowed_order)) { $order = $default_sort; } else { $order = $_POST['sortby']; } echo $order."<br>".$address."<br>".$seats."<br>".$departure; ?> <form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="post"> <p style="text-align:right"><label>Sort results by:</label> <select name="sortby" class="sort" style="width:130px;"> <option value="any"></option> <option value="start_street">Street Name</option> <option value="depart_time" selected>Departure Time</option> </select> <input type="submit" name="sortbutton" value="Sort Results" class="sendbutton" /></p> </form> <?php include_once("footer.inc.php"); ?> What about storing the variables in a session? or is there a better, much simpler way? Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/ Share on other sites More sharing options...
p2grace Posted March 23, 2008 Share Posted March 23, 2008 Sessions are the way to go, they are extremely simple. Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499013 Share on other sites More sharing options...
AdRock Posted March 23, 2008 Author Share Posted March 23, 2008 What do you reckon on this? Create a session variable from the $_POST so it doesn't get lost and then i can use the session in a database query Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499018 Share on other sites More sharing options...
p2grace Posted March 23, 2008 Share Posted March 23, 2008 Yeup, just make sure that before using it that it exists, and if it doesn't redirect them to the form. Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499021 Share on other sites More sharing options...
AdRock Posted March 23, 2008 Author Share Posted March 23, 2008 I have done that and it seems to work apart from one problem When i enter some data on the form and post it to the new page, i can see all the fields. When i refresh the page, one of the sessions isn't echoing out It's the $_SESSION['seats'] It's strange becuase the session was set when the page first loaded, but when it reloads, the session is empty for that field if (!empty($_POST['address'])) $_SESSION['address'] = $_POST['address']; if (!empty($_POST['departure'])) $_SESSION['departure'] = $_POST['departure']; if ($_POST['seats'] !="any" ) $_SESSION['seats'] = $_POST['seats']; //if ($seats =="any") $seats=""; $default_sort = 'id'; $allowed_order = array ('start_street', 'depart_time'); /* if order is not set, or it is not in the allowed * list, then set it to a default value. Otherwise, * set it to what was passed in. */ if (!isset ($_POST['sortby']) || !in_array ($_POST['sortby'], $allowed_order)) { $order = $default_sort; } else { $order = $_POST['sortby']; } echo $order."<br>".$_SESSION['address']."<br>".$_SESSION['seats']."<br>".$_SESSION['departure']; Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499031 Share on other sites More sharing options...
p2grace Posted March 23, 2008 Share Posted March 23, 2008 Is their a post variable called seats, or is the post variable = any? Your assignment and display looks correct. Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499068 Share on other sites More sharing options...
AdRock Posted March 23, 2008 Author Share Posted March 23, 2008 I have it working now. Just a problem destroying or unsetting the sessions without destroying the users login session Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499072 Share on other sites More sharing options...
p2grace Posted March 23, 2008 Share Posted March 23, 2008 Good deal Link to comment https://forums.phpfreaks.com/topic/97529-passing-variables-to-the-same-page-on-refreshreload/#findComment-499074 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.