View Full Version : This navigation is going to be the death of me...

08-24-2010, 01:27 AM
This navigation is going to be the death of me, and I think I'm sliding back to noob status....

I've done a navigation, that works just wonderfully. There are three dropdowns in the top row, and one down below. The three on the top populate the one below it.

The nav js script goes as follows:

var cacheobj=document.dynamiccombo.stage2

function populate(x){
for (m=cacheobj.options.length-1;m>0;m--)
for (i=0;i<selectedarray.length;i++)
cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)


The upper three dropdowns are set up like so:

<select name="usstage1" size="1" onchange="showDiv(this.value);window.open(this.options[this.selectedIndex].value,'_top');" >
<option selected="selected" value="javascript:populate(combo1)">United States</option>
<option value="javascript:populate(combo2)">Alabama</option>
<option value="javascript:populate(combo3)">Alaska</option>
<option value="javascript:populate(combo4)">Arizona</option>
<option value="javascript:populate(combo5)">Arkansas</option>
<option value="javascript:populate(combo6)">California</option>

Which populates the bottom dropdown with the locales of that area.

Once an option is selected in the bottom dropdown, it goes to the respective page depending on selection.

So if you select California from the upper list, the dropdown below populates the list of cities. If you select Los Angeles, it navigates to the LA page. So...none of this is the problem...the problem is, I need it to keep the last selected state of all 4 dropdown boxes as it changes pages...

I went with a couple different cookie options and failed miserably. One of them messed it all up by populating the top three boxes with the position of the last selected state of the "city" dropdown. So if I chose LA, it selected from the top 3 boxes the 5th position in their menu and then populated it with that area...anywho....

Help, a solution, or a positive kick in the bum in the right direction would be truly appreciated.

Old Pedant
08-24-2010, 08:42 PM
Sorry, that <select> makes no sense.

For starters, you have this:

<select name="usstage1" size="1"
onchange="showDiv(this.value);window.open(this.options[this.selectedIndex].value,'_top');" >

But this.value and this.options[this.selectedIndex].value are exactly the same...just two different ways of getting the same value.

But in any case, this.value (or the other expression which does that same thing) is going to get the value of the chosen <option>, right?

But *ALL* of your <option>s look like this:

<option selected="selected" value="javascript:populate(combo1)">

which means that your code ends up doing, in effect:


Now, it is *POSSIBLE* that your showDiv code *could* do an eval( ) call so that ends up invoking "populate(combo1)". But clearly the window.open is nonsense. It will *NOT* invoke eval, and even if it did I doubt seriously that "populate(combo1)" is going to produce a usable URL for the browser to open at.

So I don't understand at all how any of this "works wonderfully". I don't suppose you can show it live???

08-25-2010, 04:15 AM
Sorry, forgot to back all the way out of the disappearing trick I was doing earlier, I realized what I was doing there after I posted this. I went about it a different way.

The "showDiv(this.value);" doesn't exist in the code any longer.

That specific part of the code had nothing to do with what I was asking about here...except that I'm a dork and didn't take it out before I posted here about the issue I am having with the session cookie.

Old Pedant
08-25-2010, 09:14 AM
Well, try again. Show real code.

Better, show us a live page that we can experience for ourselves.

08-30-2010, 02:28 PM
Real code with my progress to date:

navigation code for front end:


$idx = null;

if(isset($_POST["setc"])) {
setcookie("nav_cookie", $_POST["sel"], time() + 3600 * 24 * 30);
header("location: " . $_SERVER["PHP_SELF"]);
} else if(isset($_COOKIE["nav_cookie"])) {
$idx = $_COOKIE["nav_cookie"];


<form name="dynamiccombo" id="dynamiccombo">
<select name="usstage1" size="1" id ="states" onchange="window.open(this.options[this.selectedIndex].value,'_top'); window.open(this.options[this.selectedIndex].id,'_top')" action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<option id="#" {$combo1} value="javascript:populate(combo1,'1','')">United States</option>
<option id="list,cat_id,13-alabama.php" {$combo2} value="javascript:populate(combo2,'2','')">Alabama</option>
<option id="list,cat_id,9-alaska.php" {$combo3} value="javascript:populate(combo3,'3','')">Alaska</option>
<option id="list,cat_id,15-arizona.php" {$combo4} value="javascript:populate(combo4,'4','')">Arizona</option>
<option id="list,cat_id,14-arkansas.php" {$combo5} value="javascript:populate(combo5,'5','')">Arkansas</option>
<option id="list,cat_id,16-california.php" {$combo6} value="javascript:populate(combo6,'6','')">California</option>


Trying a session cookie


var cacheobj=document.dynamiccombo.setc

function populate(x,y,z){
for (m=cacheobj.options.length-1;m>0;m--)
//var z;
cacheobj.options[0]=new Option('Please select sub location','')
for (i=1;i<selectedarray.length;i++)
if (y)
selectedarray[i].value=selectedarray[i].value + '?combo=' + y ;
cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)

if (selectedarray[i].text.toLowerCase().replace(' ','')==z.toLowerCase())




relevant part of page where the navigation takes you to:

$sublocation=ucwords(@implode(' ',$caid));

$txt[$combo] = "selected=selected";
if ($sublocation)
$txt['javascript1']="onload=populate('".$comboppp."','".$_GET['combo']."','".str_replace(' ','',trim($sublocation))."')";
//$txt['sublocation'] = 'Choose country or state';
//$txt['sublocation'] = ucfirst($sublocation);
$txt['sublocation'] = 'Please select sub location';

Current issue...It navigates to the first page (the state), but it's not holding the cookie across. if I remove


Then it populates, and you can select from the dropdown and it navigates to the city page just fine holding the cookie. But I need it to hold both when you go to the state page keeping it's location and keeping the lower box populated with the cities.

Old Pedant
08-30-2010, 09:36 PM
I just don't get this, at all:

<select name="usstage1" size="1" id ="states"
action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">

<option id="list,cat_id,13-alabama.php" {$combo2}

(1) <select>s do not *HAVE* action= or method= properties. Only <form> tags have those properties.

(2) You onchange is going to open the _top window (that is, the main window, if this code happens to be in a <frame> or <iframe>, else the same window) *TWO TIMES*. So the second window.open( ) will utterly wipe out whatever the first one does.

(3) Not that it matters, as NEITHER of those window.open() calls are going to do anything. Or at least anything you think they will do.

The first argument to window.open( ) *MUST* be a URL. Period. It can be an abbreviated URL (that is, one that is relative to the current page), but it can't be arbitrary JavaScript code.

You will end up doing (for example):

window.open( "javascript:populate(combo2,'2','')", '_top' );
and then
window.open( "list,cat_id,13-alabama.php", '_top' );

And neither of those are legitimate URLs as the first argument to window.open().

So I, for one, can't make any sense at all out of that code.

08-30-2010, 10:41 PM

window.open(this.options[this.selectedIndex].value,'_top'); and window.open


( "javascript:populate(combo2,'2','')", '_top' );

combination does exactly what it should and populates the final dropdown.

as long as I leave the id combination out, it also retains the cookie. Leaving e
the id combination in negates the dropdown, but opens the page I need it to (same window).

I need to figure out how to get it to do both.

Old Pedant
08-30-2010, 10:50 PM
Okay, I don't get it. But if it works for you, it works for you.