Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-24-2010, 12:27 AM   PM User | #1
Taraes
New Coder

 
Join Date: Aug 2010
Location: Oregon
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Taraes is an unknown quantity at this point
This navigation is going to be the death of me...

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:

Code:
var cacheobj=document.dynamiccombo.stage2

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



populate(combo1)
The upper three dropdowns are set up like so:

Code:
<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>
etc...
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.
Taraes is offline   Reply With Quote
Old 08-24-2010, 07:42 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Sorry, that <select> makes no sense.

For starters, you have this:
Code:
<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:
Code:
       <option selected="selected" value="javascript:populate(combo1)">
which means that your code ends up doing, in effect:
Code:
    showDiv("javascript:populate(combo1)");
    window.open("javascript:populate(combo1)",_top);
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???
__________________
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.
Old Pedant is offline   Reply With Quote
Old 08-25-2010, 03:15 AM   PM User | #3
Taraes
New Coder

 
Join Date: Aug 2010
Location: Oregon
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Taraes is an unknown quantity at this point
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.
Taraes is offline   Reply With Quote
Old 08-25-2010, 08:14 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Well, try again. Show real code.

Better, show us a live page that we can experience for ourselves.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 08-30-2010, 01:28 PM   PM User | #5
Taraes
New Coder

 
Join Date: Aug 2010
Location: Oregon
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Taraes is an unknown quantity at this point
Real code with my progress to date:

navigation code for front end:

Code:
<?php

   $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


js

Code:
var cacheobj=document.dynamiccombo.setc

function populate(x,y,z){
for (m=cacheobj.options.length-1;m>0;m--)
cacheobj.options[m]=null
selectedarray=eval(x)
//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())
	{
		
		cacheobj.options[i].selected=true
	}
	
}

}
relevant part of page where the navigation takes you to:

Code:
$caid=@explode('.php',$_GET['cat_id']);
$caid=@explode('-',$caid[0]);
$caid[0]='';
$sublocation=ucwords(@implode(' ',$caid));

$combo='combo'.$_GET['combo'];
$txt[$combo] = "selected=selected";
if ($sublocation)
{
	$comboppp='combo'.$_GET['combo'];
	$txt['javascript1']="onload=populate('".$comboppp."','".$_GET['combo']."','".str_replace(' ','',trim($sublocation))."')";
	//$txt['sublocation'] = 'Choose country or state';
	//$txt['sublocation'] = ucfirst($sublocation);
}
else
{
	$txt['javascript1']='';
	$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

Code:
window.open(this.options[this.selectedIndex].id,'_top')
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.
Taraes is offline   Reply With Quote
Old 08-30-2010, 08:36 PM   PM User | #6
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
I just don't get this, at all:
Code:
<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="list,cat_id,13-alabama.php" {$combo2}
        value="javascript:populate(combo2,'2','')">Alabama</option>
(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):
Code:
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.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 08-30-2010, 09:41 PM   PM User | #7
Taraes
New Coder

 
Join Date: Aug 2010
Location: Oregon
Posts: 34
Thanks: 0
Thanked 0 Times in 0 Posts
Taraes is an unknown quantity at this point
The

Code:
 
window.open(this.options[this.selectedIndex].value,'_top'); and window.open
and
Code:
 
( "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.
Taraes is offline   Reply With Quote
Old 08-30-2010, 09:50 PM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Okay, I don't get it. But if it works for you, it works for you.
__________________
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.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:23 PM.


Advertisement
Log in to turn off these ads.