Jump to content

Combining 2 scripts


DBookatay

Recommended Posts

Hello,

 

I have a script that uses a pulldown menu to send the user to the selected page. I use this script to "filter" items on the page:


function faqRedirect(selectBox) {
	var faqURL = selectBox.options[selectBox.selectedIndex].value
		if (faqURL != "")
	{
		if (faqURL.substr(0, 4) != "http")
			faqURL = "../" + faqURL
		location.href = faqURL
	}
}

<select id="filter" name="filter" onChange="faqRedirect(this)">
<option value="search.php?cat=001&filter=yearDESC">Year: Newest First</option>
<option value="search.php?cat=001&filter=yearASC">Year: Oldest First</option>
<option value="search.php?cat=001&filter=priceDESC">Price: Highest First</option>
<option value="search.php?cat=001&filter=priceASC">Price: Lowest First</option>
<option value="search.php?cat=001&filter=milesASC">Mileage: Lowest First</option>
<option value="search.php?cat=001&filter=milesDESC">Mileage: Highest First</option>
</select>

 

 

Question is: how do I modify the current java section of the script to write a cookie to remember the users selection the next time the page loads?

 

Link to comment
https://forums.phpfreaks.com/topic/118716-combining-2-scripts/
Share on other sites

Use javascript, like the code below:

 

function faqRedirect(selectBox) {
	var faqURL = selectBox.options[selectBox.selectedIndex].value
		if (faqURL != "")
{
		document.cookie="SelectBoxPref="+faqURL.replace(/=/g,"#EQUALS#");
		if (faqURL.substr(0, 4) != "http")
			faqURL = "../" + faqURL
		location.href = faqURL
}
}

var cookies = document.cookie.split(";");
for (var i=0;i<cookies.length;i++) {
if (cookies[i].indexOf("SelectBoxPref=")==-1) continue;
var selectedUrl = cookies[i].split("=")[1].replace(/#EQUALS#/g,"=");
var selectOption = document.getElementById("filter").firstChild;
while (true) {
	if (selectOption.value==selectedUrl) {
		selectOption.selected = true;
		break;
	}
	if (!selectOption.nextSibling) break;
	selectOption = selectOption.nextSibling;
}
}

Link to comment
https://forums.phpfreaks.com/topic/118716-combining-2-scripts/#findComment-612597
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.