How do yo retain a selection from a drop down list when refreshing in php

Started by
1 comment, last by Madhed 9 years, 10 months ago

This code works but for some reason if you use it in php, it will reload and take you to the top of the page when you make a selection, and I can't use that because is for a long form, and the part where I'm using this is kind of at the bottom, so it would be annoying to the user to have the page reload and take you to the top of the page everytime you make a selection, idk if maybe I could fix this but I have no idea

<!DOCTYPE HTML>
<html>
<head>


<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<script>
$("#checkAll").on("change", function() {
$(':checkbox').not(this).prop('checked', this.checked);
});


window.onload = function () {
var cookies = document.cookie,
sidx = !!cookies ? cookies.match(/myDDIdx=([0-9]+?);?/) : false;

if (!!sidx && sidx.length >= 2) {
document.getElementById("Selection").selectedIndex = sidx[1];
}
};

document.getElementById("Selection").onchange = function () {
document.cookie = 'myDDIdx=' + this.selectedIndex + '; path=/;';
window.location = this.options[[this.selectedIndex]].value;
};

</script>

<select id="Selection" class="sorter" style="float:right;margin-right:8px;">
<option value="">Sort by</option>
<option >Code</option>
<option value="?orderby=1">Title A-Z</option>
<option value="?orderby=2">Title Z-A</option>
<option value="?orderby=3">Brand</option>
<option value="?orderby=4">Lowest price</option>
<option value="?orderby=5">Highest price</option>
<option value="?orderby=6">Lowest Quantity</option>
<option value="?orderby=7">Highest Quantity</option>
</select>

Advertisement

Ugh. Why do so many people in web design try to treat web pages like heavy clients?

You are building a web page that will struggle for use on phones, tablets, and older web browsers. Code like that is why I disable scripting on most web pages, and also why businesses end up making dual sites for devices.

Sorry for the complaint rather than constructive answer, just be aware that you are likely losing or annoying 1% to 5% of your potential site users, and also potentially causing the business to invest in a duplicated effort for mobile users because of the choice to make the fancy heavily-scripted pages.

You should do the sorting via ajax or if the whole list is available, sort on the client side completely.

This topic is closed to new replies.

Advertisement