Conditional Secondary Nav Menu, Based Upon Cookie

Community Forums Forums General Discussion Conditional Secondary Nav Menu, Based Upon Cookie

This topic is: not resolved

Tagged: ,

This topic contains 1 reply, has 2 voices, and was last updated by  Susan 9 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #56980

    ItsPhipps
    Participant
    Post count: 4

    I’m looking to display the secondary nav menu on my site based upon a cookie being saved in a user’s browser.

    Now, please bare with me because I have zero experience with Javascript or PHP, but I made an honest effort to try putting this together myself before coming here for help. Hopefully my code just needs a couple small fixes and polishing up, but we’ll see lol.

    Here’s what I’m trying to accomplish…

      1. If no cookie is stored, then user sees the default secondary nav menu which links to a page with a webform.
      2. The webform will have a dropdown form so the user can choose their city and hit “submit”.
      3. Once they send the form, I will first set a cookie, then redirect to the correct city page.
      4. When they come back, the website will read the cookie, display the correct secondary nav menu, and reset the expire for the cookie.

    So Here’s The Code I Have So Far

    First is the Javascript

    <SCRIPT type=text/javascript>
    function setCookie(c_name, value, expiredays)
    {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie=c_name + "=" + escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
    }
    function CityCookieRedirect()
    {
    	var mylist = document.getElementById("myList");
    	var myCity = mylist.options[mylist.selectedIndex].text; // supposed to get the selected option text like "Orlando FL"
    	var myRedirect = mylist.options[mylist.selectedIndex].value; // supposed to get the selected option value like "orlando-fl"
    setCookie('myCity',myCity,365)
    	var url = "http://www.mysite.com/";
    	document.location.href = url  + myRedirect;
    }
    </SCRIPT>

    Second is the Form

    <form>
    <select name="myList">
      <option id="new-york-ny">New York NY</option>
      <option id ="orlando-fl">Orlando FL</option>
      <option id ="seattle-wa">Seattle WA</option>
      <option id ="san-francisco-ca">San Francisco CA</option>
      <input onclick=CityCookieRedirect() type="button" value="This Is My City">
    </select>
    </form>

    And here’s the PHP for the secondary Nav (seems like there would be a shorter way of doing this but I don’t know how)

    <?php
    if (isset($_COOKIE["new-york-ny"]))
        wp_nav_menu( array('menu' => 'new-york-ny' ));;
    else if (isset($_COOKIE["orlando-fl"]))
        wp_nav_menu( array('menu' => 'orlando-fl' ));;
    else if (isset($_COOKIE["seattle-wa"]))
        wp_nav_menu( array('menu' => 'seattle-wa' ));;
    else if (isset($_COOKIE["san-francisco-ca"]))
        wp_nav_menu( array('menu' => 'san-francisco-ca' ));;
    else
        wp_nav_menu( array('menu' => 'no-city' ));
    ?>

    So here are some of my questions…

      1. Will this even work, and can I set a cookie with Javascript and read with PHP like that?
      2. Is there a better way to get this done or am I on the right track?
      3. How can I clean up the code and make this work nicely?

    Here’s some info that will probably matter…

      I’m using Genesis 2.0
      Have HTML5 enabled
      Using factory genesis sample child theme (hasn’t really been customized yet)

    One more thing… I’m fully willing to accept PM’s from you guys with an offer to get this going for me. I have no problem paying up if there’s a lot more to getting this done than what I’m putting across here.

    Anyways, thank you for your help!!!

    Dan

    • This topic was modified 1 year ago by  ItsPhipps. Reason: making it neat
    • This topic was modified 1 year ago by  ItsPhipps. Reason: making it neat
    http://www.thehomehow.com/html5test
    #73661

    Susan
    Moderator
    Post count: 8487

    Hi – you posted this 3 months ago, so I hope you got your issue resolved. If that’s not the case, come back and let me know, and I will escalate this for you…


    Susan @ Graphically Designing I’d love to customize your website! I tweet!
    I’ve taken up the challenge! – help me answer some of the unanswered posts

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.