recover data and put them in a select

1

I have a form with a <select> with several options in it to choose the order in which the information that the user sees on the screen will be displayed:

<form method="get" action="busqueda.php" class="height-30 float-right ">
    <select id="orden"  name="orden">                            
        <option value="1">Hora: publicación más reciente</option>
        <option value="2">Precio: más bajo primero</option>
        <option value="3">Precio: más alto primero</option>
        <option value="4">Distancia: más cercano primero</option>            
        <option value="5">Relevancia</option>
        <option value="6">Urgentes</option>
    </select>
</form>

Selecting an option executes the following:

$('body').on('change', '#orden', function(){
    $(this).parents('form').submit();
});

When the user selects the option, for example, Price: lowest first the search is updated and displayed correctly in order.

What happens is that when the page is reloaded the first option that appears in the <select> is the first Time: most recent publication so if the user would like to see the information with this order I could not select it because the jQuery code only makes submit when the value of <select> changes.

How could I do so that once a value was selected, when I reloaded the page, I would show it as the first option in <select> and thus be able to choose the other options?

I attach images to detail the problem:

  • This shows the% <select> when loading the page

  • The user selects an option, for example Price: lowest first

  • When reloading the page, it continues to show this:

  • I would like this to be shown:

  • I hope you have explained me well

        
    asked by gmarsi 02.06.2017 в 15:41
    source

    3 answers

    3

    What you could do is see if the value of the select has been sent in the GET (since I see that in your form you use the GET method) using the following code:

    if(isset($_GET['orden'])){
        $orden = $_GET['orden'];
    }
    

    and at the time of showing the select check each element if it has been selected in the following way:

    <select id="orden"  name="orden">
        <option value="1" <?php echo (isset($orden) && ($orden == 1))? "selected" : ""; ?>>Hora: publicación más reciente</option>
        <option value="2" <?php echo (isset($orden) && ($orden == 2))? "selected" : ""; ?>>Precio: más bajo primero</option>
        <option value="3" <?php echo (isset($orden) && ($orden == 3))? "selected" : ""; ?>>Precio: más alto primero</option>
        <option value="4" <?php echo (isset($orden) && ($orden == 4))? "selected" : ""; ?>>Distancia: más cercano primero</option>            
        <option value="5" <?php echo (isset($orden) && ($orden == 5))? "selected" : ""; ?>>Relevancia</option>
        <option value="6" <?php echo (isset($orden) && ($orden == 6))? "selected" : ""; ?>>Urgentes</option>
    </select>
    
        
    answered by 02.06.2017 / 15:57
    source
    2

    I can think of two ways you can solve this.

  • Using Ajax to prevent your entire page from reloading.

  • That after going to the server (or doing the search on that same page), save the value of the select in some variable and then compare it in the options of the select to set a "selected".

  • If the options you show us are hard, then it will be necessary to add the condition to each of them just as shown by @Joacer.

        
    answered by 02.06.2017 в 15:59
    0

    if when choosing an option the page is reloaded it is because you are resorting to an action of the controller such that it can be "search" in your case you are collecting the parameter "order" to make a search. You only need to change the order value for the view along with the result of the search and pass it as select-value in your drop-down

        
    answered by 02.06.2017 в 15:54