Save selected cell after sending

On my site, a user is allowed to filter games by genre. When a user selects a genre from the selection window and sends requests, the page uses GET to find out what a filter is. Now the filter works fine, the problem is that the selection block is selected by default (which says "everything.")

I want that after the user has sent his filter request, the selection window will save this choice after the page is reloaded.

There is only one way I could come up with, but that would require adding all the options in PHP. Are there any simpler ways to do this using PHP or jQuery?

0
source share
3 answers

, , , , jQuery, ( cookie , bleh).

PHP option echo selected="selected", .

, - :

<select name="test">
<?php
$options = array(1 => 'Option 1', 2 => 'Option 2', 3 => 'Option 3');
foreach ($options as $key => $value) { 
   echo '<option value="' . $key . '"' . ($key == $_GET["test"] ? ' selected="selected"' : '') . '>' . $value . '</option>';
} ?>
</select>
+2

jQuery. .

PHP . , selected.

.

foreach ($options as $value => $label) {
    echo '<option value="' . $value . '" ' . ($selected == $value ? ' selected' : '') . '>' . $label . '</option>';
}
+6

you can do this without a loop, just change the value of 6 to whatever value you want when rendering the page

$ ("# $ optionId [value = 6]"). attr ('selected', 'selected');
+1
source

Source: https://habr.com/ru/post/1796685/


All Articles