www.webdeveloper.com
Results 1 to 2 of 2

Thread: Populate a drop down with Cookie.

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    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>
    Can someone help me with this please? Thanks.

  2. #2
    Join Date
    Nov 2010
    Posts
    1,276
    here is a function you can call to populate a dropdown list with a query string:

    Code:
    <body>
    <form name="myform">
    <select name="selectlist">
    </select>
    </form>
    
    <script type="text/javascript">
    function makeList(){
    var refer = unescape(self.location.search.slice(1));
    document.myform.selectlist.options[0]=new Option(refer,refer);
    }
    </script>
    </body>
    Last edited by xelawho; 01-03-2012 at 05:01 PM. Reason: didn't notice it had to be selected

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center

"

"

X vBulletin 4.2.2 Debug Information

  • Page Generation 0.22555 seconds
  • Memory Usage 2,850KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (2)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates