1

Possible duplicate:

Hey there, I am building a form which is used to query an API and filter some search results. None of this data is being stored in a text file or a database.

Naturally the form submission refreshes the page, and the form values default to the native selected options I specified when writing the HTML. I am looking to store the users submitted values and have those be the selected options in the form after the page reloads and the filtered selections are loaded. What would be the best solution for achieving this? Sessions, or querying the POST array?

Naturally, I did a couple of Google Searches but everything related to databases or text files, which didn't apply.

My current form code:

<form method="post" action="">
  <label>Year</label>
  <select class="minYear" name="minimuimYearFilter">
    <?php 
    $vehicleYear = date("Y") + 1;
    for ($i = 1993; $i <= $vehicleYear; $i++) {
        echo "<option value=".$i.">".$i."</option>";
    }
    ?>
  </select>
  To:
  <select class="maxYear" name="maximumYearFilter">
    <?php 
    for ($i = 1993; $i <= $vehicleYear; $i++) {
      if ($i == $vehicleYear) {
        echo "<option value=".$i." selected>".$i."</option>"; 
      } else {
        echo "<option value=".$i.">".$i."</option>";
      }
    }
    ?>
  </select>
  <label for="priceFilter">Price</label>
  <select class="priceFilter" name="priceFilter">
    <?php 
    for ($i = 1000; $i <= 50000; $i=$i+1000) {
      if ($i == 50000) {
        echo "<option value=".$i.">$".$i."+</option>";
      } elseif ($i == 20000) {
        echo "<option value=".$i." selected>$".$i."</option>";
      } else {
        echo "<option value=".$i.">$".$i."</option>";
      }
    }
    ?>
  </select>
  <label for="mileageFilter">Mileage</label>
  <select class="mileageFilter" name="mileageFilter">
    <?php 
    for ($i = 5000; $i <= 100000; $i=$i+5000) {
      if ($i == 100000) {
        echo "<option value=".$i.">".$i."+</option>";
      } elseif ($i == 75000) {
        echo "<option value=".$i." selected>".$i."</option>";
      } else {
        echo "<option value=".$i.">".$i."</option>";
      }
    }
    ?>
  </select>
  <input type="submit" value="Submit">
</form>

Hey all, have a meeting, then out of town beyond wifi over the weekend, will catch up ASAP on Monday. Thanks for all the great responses thus far! Cant wait to get back and try this.

robabby
  • 2,160
  • 6
  • 32
  • 47
  • Session. POST is lost when he moves to another page. But use separate echo-ed values with , not . as . concatenates them in vain while , just outputs them one by one. It's a special PHP echo gimmick. – CodeAngry Oct 19 '12 at 20:53

5 Answers5

2

If you mean passing data between pages - you should read best answer for this question:

PHP Pass variable to next page

After this - read more about sessions in PHP. Session is the best mechanism for "remembering application state".

Community
  • 1
  • 1
Kamil
  • 13,363
  • 24
  • 88
  • 183
  • That makes sense, though I was attempting to steer clear of having to implement sessions, that may have to be the route I take. Thanks! – robabby Oct 19 '12 at 20:59
  • Sessions are not so scary. They are easier to use that you may think :) – Kamil Oct 19 '12 at 21:06
  • Haha, I have done some work with sessions before, and I was hoping there may be another 'simpler' route to implementing a session since this is the only doc that the session would apply to in my whole site. But I catch your drift :) Thanks again! – robabby Oct 19 '12 at 21:11
1

The fact that you have are using select boxes makes this harder, but the general idea is to use the value of each select box in the $_REQUEST array to decide which option gets the selected attribute.

Something along these lines:

for ($i = 1993; $i <= $vehicleYear; $i++) {
    echo "<option value='".$i."'".(($_REQUEST['minimuimYearFilter']==$i)?"selected":"").">".$i."</option>";
}

The idea is that the option that was previously selected gets a selected attribute, making it the default selected value.

This should work out of the box, at least in my example, the reason being that if no value is selected, none of them get a selected attribute and the top one is the default. If you also want a 'first time' selected option, simply detect if $_REQUEST['whatever'] has returned anything, and if not set its value to the option you want to be selected.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Right, I think that makes sense. Especially since I am dynamically generating these long select options, generating the proper select element to echo out `selected` too seems to be one the biggest hurdles in this use-case... – robabby Oct 19 '12 at 20:56
  • I have updated my question with an example, let me know if you need further details. – Asad Saeeduddin Oct 19 '12 at 21:00
  • No I really like the solution you have here. I'm letting it marinate and I may try implementing. Would there be any special considerations with your snippet to look out for on the initial page load, prior to user interaction, or do you think this would work 'out of the box'? – robabby Oct 19 '12 at 21:02
  • Very cool, I am going to try this solution first, though I have a meeting, and out of town till Monday, so may have to wait till then to test (and Accept :)). Thanks again! – robabby Oct 19 '12 at 21:09
1

First on top page set session:

session_start();

For example if $_POST is sent than set in a session:

if (isset($_POST['vehicle_selected']))
{
    $_SESSION['vehicle_selected'] = $_POST['vehicle_selected'];
}
else
{
    $_SESSION['vehicle_selected'] = ''; 
}

In HTML form add this value:

value="<?=$_SESSION['vehicle_selected']; ?>";

Or full example:

<?php

session_start();

if (isset($_POST['vehicle_selected']))
{
    $_SESSION['vehicle_selected'] = $_POST['vehicle_selected'];
}
else
{
    $_SESSION['vehicle_selected'] = ''; 
}

?>

    <form method="post" action="">
      <label>Year</label>
      <select class="minYear" name="minimuimYearFilter">
        <?php 
        $vehicleYear = date("Y") + 1;
        for ($i = 1993; $i <= $vehicleYear; $i++) {
        echo "<option value=".$_SESSION['vehicle_selected'].">".$_SESSION['vehicle_selected']."</option>";
        }
        ?>
      </select>
    ....

Another words (not answers, but recommended for security purposes):

Using REQUEST method can be attack simpler than POST, instead of REQUEST ($_GET,$_POST) use only $_GET. If you want more secure explore CSRF protection for PHP in Stackoverflow. But this is only for security.

Marin Sagovac
  • 3,932
  • 5
  • 23
  • 53
  • I appreciate the response here, and I love the security consideratiosn you brought up. I may not have enough time today to plug in your solution, but If I miss it tonight, I will try on Monday. Cheers! – robabby Oct 19 '12 at 21:07
0

Use cookies. The PHP man page for setcookies() is pretty self-explanatory: http://php.net/manual/en/function.setcookie.php

user428517
  • 4,132
  • 1
  • 22
  • 39
  • $_COOKIE can only contain 4kB of data: Enough for small forms, but if you have a content field you can quickly pass this. PHP doesn't really give an error when you go past the 4kB limit, so it looks like cookies are not working and this is super frustrating. Therefore I would not suggest to use cookies to store form data, unless it is something like the e-mail in a login form – Tessmore Oct 19 '12 at 21:03
  • @Tessmore I agree, I feel like cookies are a stretch for this use-case. Thanks sgroves for the response though. – robabby Oct 19 '12 at 21:04
  • You have four dropdowns, each of which could default to one numerical value. What do you need to store that's over 4K? Maybe I misread your question, but it looks like this is exactly the sort of thing you'd use cookies for. – user428517 Oct 19 '12 at 21:13
0

I would use the POST array. session only adds a step that isn't necessary if you arn't using the inputs across multiple pages.

$value = (isset($_POST['some_option']) ? $_POST['some_option'] : "";
?>

<select selected="<?php echo $value; ?>">

or

<input value="<?php echo $value; ?>"/>
grasingerm
  • 1,955
  • 2
  • 19
  • 22