Jump to content

Passing variables to the same page on refresh/reload


AdRock

Recommended Posts

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?

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'];

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.