...

View Full Version : Populate a drop down with Cookie.



misslilbit02
01-03-2012, 08:26 PM
I really need help with this. I really don't know how to make this work. I need to populate a drop down menu with a value passed through the browser if it's passed.

so the browser URL will look like:
domain.com/?referer=YellowPagesOnline

I need to capture the referer value from the browser store it into a cookie. If the cookie is set and then populate the drop down on the page with the cookie. If it's not set then a list of sources should appear.

So I already have the code to grab the string and store it in a cookie but I'm not sure how to do the initialization of the drop down with the cookie value.


This is what I have...



<script>
//Get the referer string out of the URL
function getQuerystring(key, default_)
{

if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}


//Set the cookie for 30 days
function SetCookie(cookieName,cookieValue,nDays) {
var today = new Date();
var expire = new Date();
if (nDays==null || nDays==0) nDays=1;
/* expire.setTime(today.getTime() + 3600000*24*nDays);*/
expire.setTime(today.getTime() + (1000 * 60 * 60 * 24 * 30));
document.cookie = cookieName+"="+escape(cookieValue)
+ ";expires="+expire.toGMTString();
}


//Put the cookie on the user's computer
function putcookie()
{
var val = getQuerystring('referer');

if(val !=""){
if (document.cookie.length == 0)
{
SetCookie('referer', val, 1);
alert(val);
}
}
alert(document.cookie);

}
putcookie();




getCookie(name);

//If there is a cookie then get the cookie
function getCookie(c_name)
{

if (document.cookie.length>0)
{
c_start=document.cookie.indexOf(c_name + "=");

if (c_start!=-1)
{
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);

if (c_end==-1) c_end=document.cookie.length;

if(document.SECatJax.referer.length > 0)
{
document.SECatJax.referer.value = unescape(document.cookie.substring(c_start,c_end));
return false;
}
alert(document.cookie);

}
}
document.SECatJax.referer.value = 'Website';
}



The dropdown to appear if there isn't a cookie is

<select name="source_code" size="1" id="source_code" class="select_2" style="display:none">
<option value="" selected="selected">--</option>
<option value="Direct Mail">Direct Mail</option>
<option value="High School Presentation">High School Presentation</option>
<option value="Search Engine">Internet Search</option>
<option value="Newspaper">Newspaper</option>
<option value="Poster">Poster</option>
<option value="Radio">Radio</option>
<option value="Referral">Referral</option>
<option value="TV">TV</option>
<option value="Yellow Pages">Yellow Pages</option>
</select>

The drop down to appear if there is a cookie is[CODE]<select name="source_code2" size="1" id="source_code2" class="select_2" style="display:none">
<option value="YellowPagesOnline" selected="selected">YellowPagesOnline</option>
</select>[CODE]

Can someone help me with this please? Thanks.

Old Pedant
01-03-2012, 10:06 PM
This seems like a big mistake, to me:


if (document.cookie.length == 0)
This would mean that if there is any other cookie for your domain, you would *not* set the cookie in place.

You might well have other pages and/or links that are setting cookies for other purposes (maybe Google code, for example?) and that would prevent the cookie from ever being set.

I see no reason for that "if" test to be there.

Old Pedant
01-03-2012, 10:07 PM
Question: If the cookie is set do you *always* want YellowPagesOnline to be selected? Or does the particular selection depend on the cookie?

misslilbit02
01-03-2012, 10:27 PM
I want the drop down selection to change based on the cookie that is stored on the computer at that time.

misslilbit02
01-03-2012, 10:30 PM
my thought was since the cookie was supposed to be set for 30 days if it exist on the computer then don't set a new. If it didn't I wanted it to set the cookie. So I'm sure my logic is off as well as programming.

Old Pedant
01-04-2012, 04:21 AM
my thought was since the cookie was supposed to be set for 30 days if it exist on the computer then don't set a new.

Fine. But then you must check for *THAT SAME* cookie. Not just any cookie.

But I don't see what harm there is in setting a new one.

Old Pedant
01-04-2012, 04:26 AM
Part of the problem here is that you have taken the getCookie() function and mixed all kinds of other stuff in with it.

And that other code isn't going to work, because you call the getCookie() function *BEFORE* the page is fully set up.

Really, you have made this overly complex by mixing things together.

misslilbit02
01-04-2012, 02:24 PM
Part of the problem here is that you have taken the getCookie() function and mixed all kinds of other stuff in with it.

And that other code isn't going to work, because you call the getCookie() function *BEFORE* the page is fully set up.

Really, you have made this overly complex by mixing things together.

Ok I was noticing when I added the getCookie that nothing would work. So forgetting all of the other stuff. Let's just say I set up the cookie and just want the drop down to populate with the cookie set how do I do that. That I don't know how to do.

Because in php I echo the select and the cookie as an option but I don't know how this is accomplished in javascript as I don't really write in this language.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum