There is a filter form in it provides a choice of tire characteristics, by pressing the button to pick up, the page reloads and gives the corresponding request, but the selected value is not displayed in the filter form. How to display text in the selection form after reloading the page?

[Пример формы][1] [1]: https://jsfiddle.net/nppkLmfr/ 

    3 answers 3

    If POST is used to submit the form, enter the value = '". $ _ POST [' test ']."' In the value property of the required input where test is the name of the desired input. If GET is used, then $ _GET ['test']

    update: If you use selects, then in the place where they are generated, you need to add an option tag

      if ('Значение данного option'==$_POST['test']){ echo ' selected ';} 

    An example for your code. (Provided that you do not use a generator for this form, a template engine or ajax)

     <select class="btn dropdown-toggle selectpicker btnfull btn-warning" name="filter_высота" size="1"> <option <?php if ($_POST['filter_высота']==40){ echo ' selected '; } ?> value="40">10.50</option> <option>....</option> ... </select> 
    • Alas, inserting your example, the site is not responding - Ivan Kling
    • Forgot to close the tag. ?> added after} - Victor Zharikov
    • And maybe you should not use Russian letters in the name. Not sure that they will work correctly everywhere - Victor Zharikov
    • Alas, again, saving does not happen - Ivan Kling
    • Throw off the properties <form> - Victor Zharikov

    In your case it will be more correct to use asynchronous requests ( AJAX ). Then you do not have to reload the entire page, and you can only update the data that came in the request.

    If you do not have this option, you can return the filter value in response to the request.

    • Initially, only the data was updated, but the client does not like it, he wants to select the data and then click the pick button - Ivan Kling
    • data refers to the results of the query. You can send a request in the same way, but not refresh the page entirely. - Alexey Prokopenko

    One option on pure js. Form data can be saved in a special storage browser localStorage .

    For example:

     var form = document.getElementById('filter_form'); function loadData(){ var data = JSON.parse(localStorage.getItem(form.id)); if(data != null){ // проверка на null и undefined form.elements.forEach(function(element){ element.value = data[element.name]; }); } } function saveData(){ var data = {}; form.elements.forEach(function(element){ data[element.name] = element.value; }); localStarage.setItem(form.id, JSON.stringify(data)); } loadData(); form.addEventListener('submit', saveData, false); 
     <form id="filter_form" action="/" mthod="GET"> <select class="btn dropdown-toggle selectpicker btnfull btn-warning" name="filter_ширина" size="1"> <option value="">Все</option> <option value="63">31</option> <option value="64">32</option> <option value="65">33</option> <option value="66">34</option> <option value="45">165</option> <option value="46">175</option> <option value="47">185</option> <option value="48">195</option> <option value="49">205</option> <option value="50">215</option> <option value="51">225</option> <option value="52">235</option> <option value="53">245</option> <option value="54">255</option> <option value="55">265</option> <option value="56">275</option> <option value="57">285</option> <option value="58">295</option> <option value="68">315</option> </select> <div class="vis"> <div class="name">Высота</div> <select class="btn dropdown-toggle selectpicker btnfull btn-warning" name="filter_высота" size="1"> <option value="">Все</option> <option value="40">10.50</option> <option value="41">11.50</option> <option value="42">12.50</option> <option value="43">35</option> <option value="75">40</option> <option value="37">45</option> <option value="38">50</option> <option value="36">55</option> <option value="62">60</option> <option value="34">65</option> <option value="35">70</option> <option value="39">75</option> </select> </div> <div class="prof"> <div class="name">Профиль</div> <select class="btn dropdown-toggle selectpicker btnfull btn-warning" name="filter_диаметр" size="1"> <option value="">Все</option> <option value="21">R13</option> <option value="22">R14</option> <option value="23">R15</option> <option value="24">R16</option> <option value="25">R17</option> <option value="26">R18</option> <option value="27">R19</option> <option value="28">R20</option> </select> </div> <div class="sez"> <div class="name">Сезон</div> <select class="btn dropdown-toggle selectpicker btnfull btn-warning" class="bu" name="filter_cезон" size="1"> <option value="">Все</option> <option value="19">Лето</option> </select> </div> <div class="bre"> <div class="name">Бренд</div> <select class="btn dropdown-toggle selectpicker btnfull btn-warning" name="filter_бренд" size="1"> <option value="">Все</option> <option value="61">BFGoodrich</option> <option value="69">Michelin</option> </select> </div> <input class="btn btn-warning pf-black" id="bu" type="submit" value="Подобрать" /> </form> 

    Unfortunately, this code cannot be run here, because the sandbox does not work with localStorage for security purposes.