Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

nicholas maddren
12,793 PointsPHP: How to save select element options
Hey guys, just have a question.
I have been trying to find out all day how I can save select options that a user on my site selects.
So let's say I have a form that includes four select elements like this:
<form class="car-finder-container" method="GET" action="used-cars.php">
<select name="make" class="form-control make-select select-box">
<option value="make-any">Make (Any)</option>
<?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC))
{
echo '
<option value="'.$make["Make"].'">'.$make["Make"].'</option>
';
} ?>
</select>
<select name="min-price" id="priceMax" class="select-box form-control price-min price-selector">
<option value="min-price">Min price</option><option value="0">Ā£0</option><option value="500">Ā£500</option>
</select>
<select name="max-price" id="priceMin" class="select-box form-control price-max price-selector">
<option value="max-price">Max price</option><option value="0">Ā£0</option><option value="500">Ā£500</option>
<button type="submit" class="btn btn-block car-search-button btn-lg btn-success"><span class="glyphicon car-search-g glyphicon-search"></span> Search cars
</button>
</form>
So basically this form displays all of the manufacturers in my database, it also includes includes a max price and min price select element that determines what is displayed on my page using the magic of jQuery.
So this form displays on my home page however does not filter any results on there, I have another page called used-cars.php.
At the moment I am using method GET to send the option selected from the 'Make' select element.
This works however is probably not a good idea as I need my whole form to have the options saved, I'm guessing a PHP cookie would be the best bet however I don't have a clue where to start.
The reason I need all of the form options that are selected to be saved is because of the nature of the form, users will be coming and going from the main search page so the GET method isn't really a method that will work for my needs.
The form I have on my used-cars.php is slightly different the select elements are named the same and have the same values however the form has some extra select elements, I'm unsure if this will make a difference.
So could someone show me a detailed example of how this can be done bearing in mind there are two forms but have the same classes and the select elements have identical attributes.
As I said is this possible with a PHP cookie? Also how can I reset the cookie when a user changes the options selected?
Thanks
1 Answer

Casey Ydenberg
15,622 PointsI don't think I quite understand your question. If I can paraphrase (tell me if I go wrong), the main page and the user-cars page contain a search form. The purpose of both forms is to execute a search. If the user submits either form, they are directed to used-cars where the search is performed based on GET variables, and the results are displayed.
The problem is that, at page load, you would like the form on the used-cars page to be pre-set with the variables user submitted, and not go back to the "default" settings.
Is that right? If it is, the good news is that is pretty simple and doesn't require anything as complicated as cookies (or sessions).
The selected attribute (http://www.w3schools.com/tags/att_option_selected.asp) allows to specify the option in a select list that is selected when the page loads. So we just change your code above:
<select name="make" class="form-control make-select select-box">
<option value="make-any" <?php if ($_GET["make"] == 'make-any') echo 'selected'; ?>>Make (Any)</option>
<?php while($make = $makeFilter->fetch(PDO::FETCH_ASSOC))
{
echo '<option value="'.$make["Make"].'"';
if ($_GET["make"] == $make["Make"]) echo 'selected';
echo '>'.$make["Make"].'</option>';
} ?>
</select>
Granted I'm not a fan of this type of code with all the if statements in the middle of HTML. I'd probably write a function to spit out each option, and use sprintf in place of all that concatenation. But that's a matter of taste.