If you need to implement sorting, or rather, if you need to “remember” the value for one or a small number of blocks, you can use cookie / Then the code on js will be:
NodeList.prototype.forEach = Array.prototype.forEach; function setCookie(name, value, options) { // функция установки cookie options = options || {}; var expires = options.expires; if (typeof expires == "number" && expires) { var d = new Date(); d.setTime(d.getTime() + expires * 1000); expires = options.expires = d; } if (expires && expires.toUTCString) { options.expires = expires.toUTCString(); } value = encodeURIComponent(value); var updatedCookie = name + "=" + value; for (var propName in options) { updatedCookie += "; " + propName; var propValue = options[propName]; if (propValue !== true) { updatedCookie += "=" + propValue; } } document.cookie = updatedCookie; } /*Обработка нажатия на блок*/ var divs = table.querySelectorAll(".sort"); divs.forEach(function(el, iNum){ el.addEventListener("click", function(e){ addActiveClassToCookie(el.innerText, iNum); }); }); function addActiveClassToCookie(data, num){ // сохранение значений в cookie setCookie("sortActiveValue", data, { expires : 60*60*24*365, // ставим куку на год domain : window.location.host // ставим доступной для текущего домена }); setCookie("sortActiveNum", num, { expires : 60*60*24*365 }); }
In php, you can get these values and display the appropriate class:
$sortActiveValue = $_COOKIE["sortActiveValue"]; $sortActiveNum = $_COOKIE["sortActiveNum"];
But do not forget to check that the data in the cookie is safe.
The most important thing
If you need to write data for a large number of classes (you had to do this a couple of times), then you need to store the data in the browser due to the fact that the cookies are transmitted in each request to the server and can significantly “burden” it.