Solarplex Solarplex - 5 months ago 17
HTML Question

Retain submitted data through refreshes

I want to retain

$orderby
and
$orderSort
(within the query) when I refresh the page or press the links at the bottom of the page to move to next page or previous page.

I have tried attaching
orderby
and
orderSort
to sessions but that didn't work due to the query - it always reverts back to Name and ASC from the else statement which seems like an issue with
isset($_POST['submit'])
but I have tried without it the same problem still occurs.

How do I retain my query (so long as the selections are not changed) as I go to the next page or previous page?

Top PHP portion:

//Next Page
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
$startrow = 0;
} else {
$startrow = (int)$_GET['startrow'];
}
//when options are selected/submitted
if(isset($_POST['submit']))
{
if($_POST['select'] == "EventType")
{
$orderby = $_POST['select'];
if($_POST['otherType'] != "Select an Event") {
$EventType = 'WHERE `' . $orderby . '`="' . $_POST["otherType"] . '"';
}
}
else
{
$orderby = $_POST['select'];
}
$orderSort = $_POST['agree'];
}
else
{
$orderby = "Name";
$orderSort = "ASC";
}

if ($stmt = $local->prepare("SELECT * FROM importdata $EventType ORDER BY `$orderby` $orderSort LIMIT ?,10")) {
$stmt->bind_param('i', $startrow);
$stmt->execute();
$Recordset1 = $stmt->get_result();
$row_Recordset1 = $Recordset1->fetch_assoc();
$stmt->close();
}


Submit Form:

<form id="form1" name="form1" method="post" enctype="multipart/form-data">
<label for="select">Order By:</label>
<select name="select" id="selectType">
<option value="EventType">Event</option>
<option value=" Name"selected="selected">Race Name</option>
</select>
<label><input type="radio" name="agree" value="ASC" checked>Ascend</label>
<label><input type="radio" name="agree" value="DESC">Descend</label>

<input type="submit" name="submit" value="Submit">
</form>


Bottom PHP for next page/previous page

<?php
$prev = $startrow - 10;
if ($prev >= 0)
echo '<a href="'.htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8").'?startrow='.$prev.'">Previous </a>';
echo '<a href="'.htmlspecialchars($_SERVER["PHP_SELF"], ENT_QUOTES, "utf-8").'?startrow='.($startrow+10).'"> Next</a>';
?>

Answer

I suggest something like this:

// Get our defaults for these variables.
$orderBy = isset($_SESSION['orderBy']) ? $_SESSION['orderBy'] : 'Name';
$orderSort = isset($_SESSION['orderSort']) ? $_SESSION['orderSort'] : 'ASC';

// When options are selected/submitted
if (isset($_POST['submit'])) {
    if ($_POST['select'] == "EventType") {
        $orderby = $_POST['select'];

        if ($_POST['otherType'] != "Select an Event") {
            $EventType = 'WHERE `' . $orderby . '`="' . $_POST["otherType"] . '"';
        }
    } else {
        $orderby = $_POST['select'];
    }

    $orderSort = $_POST['agree'];

    // Set our session variables for next time when we set our defaults.
    $_SESSION['orderBy'] = $orderBy;
    $_SESSION['orderSort'] = $orderSort;
}
Comments