Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2005
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Populate a drop down with Cookie.

    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...

    Code:
    <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
    Code:
    <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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    This seems like a big mistake, to me:
    Code:
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    misslilbit02 (01-03-2012)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Question: If the cookie is set do you *always* want YellowPagesOnline to be selected? Or does the particular selection depend on the cookie?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    misslilbit02 (01-03-2012)

  • #4
    New Coder
    Join Date
    Jul 2005
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Response

    I want the drop down selection to change based on the cookie that is stored on the computer at that time.

  • #5
    New Coder
    Join Date
    Jul 2005
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Response2

    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.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    Quote Originally Posted by misslilbit02 View Post
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,550
    Thanks
    78
    Thanked 4,382 Times in 4,347 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    New Coder
    Join Date
    Jul 2005
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •