0

I have a website that shows users data based on the State they live in. There is a filter via a drop-down list that allows them to select any State or all States. The current default selection is to show them the data from THEIR State.

I want to change the default to show all States (no filter). There is a list item at the top called "Alle" that does this, but I don't know how to make this the default when the webpage loads.

Here's the current code:

<label class="control-label" for="Bundesland">Bundesland:</label>
<div class="controls">
    <select class="span12" id="Bundesland" name="filter[bundesland]">
        <option value="">Alle</option>
                        <?php
                            $a = [
                            '01' => 'Schleswig-Holstein',
                            '02' => 'Hamburg',
                            '03' => 'Niedersachsen',
                            '04' => 'Bremen',
                            '05' => 'Nordrhein-Westfalen',
                            '06' => 'Hessen',
                            '07' => 'Rheinland-Pfalz',
                            '08' => 'Baden-Württemberg',
                            '09' => 'Bayern',
                            '10' => 'Saarland',
                            '11' => 'Berlin',
                            '12' => 'Brandenburg',
                            '13' => 'Mecklenburg-Vorpommern',
                            '14' => 'Sachsen',
                            '15' => 'Sachsen-Anhalt',
                            '16' => 'Thüringen',
                            ];
                            asort($a);
                        ?>
                        <?php foreach ($a as $code=>$name): ?>
                        <option value="<?=$code?>" <?=$code == $this->filter['bundesland'] ? 'selected' : ''?>><?=$name?></option>
                        <?php endforeach ?>
    </select>

Screenshot of drop-down list:

Screenshot of drop-down list

Peter Lairo
  • 331
  • 2
  • 8

1 Answers1

0

HTML's element: has an attribute; 'selected'. At the page load, make the first one 'selected'. Read more about it here: http://www.w3schools.com/tags/att_option_selected.asp

 <option value="" selected>Bitte wählen</option>
Tosfera
  • 512
  • 3
  • 14
  • That doesn't work. The solution might require AJAX. Desired result: on page load: Filter = "Alle". I only know some CSS and zero Javascript. Could someone please kindly provide the needed code? – Peter Lairo Jan 13 '15 at 09:25
  • So, at the load of the page you want to filter it directly? – Tosfera Jan 13 '15 at 09:28
  • At the load of the page, it should show the results for **all** German states. It should show the same results as when I manually select "Alle" from the dropdown list. I don't know if that is a "filter" or "no filter". :-\ – Peter Lairo Jan 14 '15 at 18:01
  • It's likely that the default filter is set by (or read from) the database and/or the user's profile. I want to override that setting using the code above (until I have many more users). – Peter Lairo Jan 15 '15 at 13:11
  • Your answer might be here: http://stackoverflow.com/questions/3518002/how-to-set-default-value-for-html-select-element – Tosfera Jan 15 '15 at 13:12
  • That would be the answer if the **PHP code** weren't there. Somehow, the `selected` in the PHP code **might** need to be removed and perhaps somehow added to the ``. I think the `value` parameter there might be relevant too (for the PHP selection). – Peter Lairo Jan 15 '15 at 14:40
  • Is there something I can do to increase the likelihood that someone will answer this question? – Peter Lairo Feb 22 '15 at 16:56