Acidic Cloud
Acidic Cloud

Reputation: 607

How do I carry search parameter values on another page in PHP

I've got an advanced parameterized query, the user sets the parameters and accordingly the query is build. I point the action of the form to a result page and on that page I fetch values using $_POST.

I get these values correctly but the moment I refresh the page I lose the values of $_POST. Do I need to use $_SESSION for this? Because I don't want so many sessions, then I had to unset all once the user has used them, so how do I do it?

Form

<form action="result.php?script_id=3&sub_script_id=1">
<label for="search_for">Search For</label>
<select id="search_for" name="contact_first_name">
<?php
    while($throw_names = mysqli_fetch_array($fetch_advance_param_values)) {
?>
    <option value="<?php echo $throw_names['contact_first_name']; ?>"><?php echo $throw_names['contact_first_name']; ?></option>
<?php
    }
?>
</select>
<br />
</form>

results.php

<?php
$fetch_results = mysqli_query($running_db, 
"SELECT rec_num, record_locked, contact_first_name, contact_last_name,
contact_mobile_no, contact_office_no FROM tbl_contacts WHERE 
contact_first_name = '{$_POST['contact_first_name']}' 
AND group_id_entry={$_SESSION['user_group_id']} 
ORDER BY $sort_column_name $sort_order $limit_records");
?>

Don't worry, right now am directly using $_POST['contact_first_name'] in my query, this is just for test purpose, later I'll use mysqli_real_escape_string()

Upvotes: 0

Views: 152

Answers (2)

Fabian Schmengler
Fabian Schmengler

Reputation: 24576

I don't get why you are concerned about using sessions or GET parameters. A search form is usually implemented with GET anyways.

But there is another option: save the search itself in the database (then a POST form actually makes sense):

  1. search form gets submitted
  2. save search query as new row in a searches table
  3. redirect to something like /searches/?id=1234
  4. on that page, rebuild the search query from the searches table and execute it

At first glance this solution looks a bit overkill but it offers new possibilities like caching a search result or offer the user to save favorite searches. So there are scenarios where this approach makes sense.

Upvotes: 1

Tschallacka
Tschallacka

Reputation: 28742

In the first page where the post is sent to:

setcookie('searchquery','I want cookies');

in page 2

if(isset($_COOKIE['searchquery']))
    {
    $search = mysqli_real_escape_string($link,$_COOKIE['searchquery']);
    }

Upvotes: 0

Related Questions