The easiest way is to use cookies. When a user submits a form, we write his choice in cookies and then with js (this is an option if you don’t have ajax implemented - which is most likely the case), we look at the cookies and fill out a quick form on their basis.
On the page connect the jquery library and ваш js файл . Download the library and create the file.
<head> <script src="js/jquery-2.2.0.min.js"></script> <script src="js/test.js"></script> </head>
When a form is submitted, on the php side they start the session and write the user's choice in cookies. For example, we write down the sex ( sex ) that was selected. Instead of the values sex and 2 you can of course use php variables. It’s only important that there are no spaces anywhere inside.
<?php //session_start(); setcookie('sex', '2', time()+3600, '/'); //сохраняем куки на один час ?>
User page reloads. It displays the search results, and at the top is the same search form. We fill it with values from cookies using jQuery (our test.js file)
jQuery(document).ready(function(e) { //После загрузки страницы получаем значение куки c именем sex sex=getCookie("sex"); //устанавливаем значение выпадающего списка c id sex-selector if (sex) { //если в переменной не пусто jQuery("#sex-selector").val(sex); } //Функция получения значения нужной куки function getCookie(cname) { name = cname + "="; ca = document.cookie.split(';'); for(i = 0; i < ca.length; i++) { c = ca[i]; while (c.charAt(0)==' ') { c = c.substring(1); } if (c.indexOf(name) === 0) { return c.substring(name.length,c.length); } } return ""; } });//конец ожидания DOM
It would be more beautiful to do everything, of course, without reloading the page, etc. using ajax, but also so well.