Community Forums › Forums › Archived Forums › General Discussion › Conditional Secondary Nav Menu, Based Upon Cookie
Tagged: menu, navigation
- This topic has 1 reply, 2 voices, and was last updated 10 years, 5 months ago by Susan.
-
AuthorPosts
-
August 16, 2013 at 10:02 pm #56980ItsPhippsMember
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
http://www.thehomehow.com/html5testNovember 17, 2013 at 1:15 pm #73661SusanModeratorHi - 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...
-
AuthorPosts
- The forum ‘General Discussion’ is closed to new topics and replies.