Zomfire
Zomfire

Reputation: 193

HTML <Select> option is changing after reload

I have a tiny problem with my select. When I set second, third or another option other than first, my select is set to first option after page reload. I want to save the choosed option. Here is the code:

echo '<select name="sort" id="sort" onchange="document.location = this.value">';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=nameup">Sort by name up</option>';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=namedown">Sort by name down</option>';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=priceup">Sort by price up</option>';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=pricedown">Sort by price down</option>';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=amountup">Sort by amount up</option>';
echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=amountdown">Sort by amount down</option>';
echo '</select>';

Upvotes: 0

Views: 632

Answers (1)

Dave Goten
Dave Goten

Reputation: 701

Your issue is that your page is reloading and the first option will always be auto selected no matter what you selected.

You need to add some logic in your PHP that says if this one was selected set this option to selected="selected"

For example:

echo '<option value="?kategoria='.$_GET['kategoria'].'&sort=amountup"';
if ($_GET['sort'] == 'amountup') {
   echo ' selected="selected"';
}
echo '>Sort by amount up</option>';

You can clean this up some but that's the basic gist of it.

Upvotes: 2

Related Questions