Grandbragus
11-06-2003, 06:20 PM
Hi there!
I've got a little problem executing a script that triggers an option menu to display different types of items depending on your latest choice...
The script goes as follows:
<html>
<head>
<script language='JavaScript'>
var store = new Array();
store[0] = new Array(
'Gold','gold','Food','food','Wood','wood','Stone','stone','Iron','iron');
store[1] = new Array(
'Irregulars','irregular','Pikemen','pikeman','Swordmen','swordmen','Cavaliers','cavalier','Longsword men',
'longswordman','Archers','archer','Crossbowmen','crossbowman','Longbowmen','longbowman','Fastriders' ,'fastrider');
store[2] = new Array(
'Daggers','dagger','Pikes','pike','Lances','lance','Swords','sword','Longsword','longsword','Bows',' bow',
'Crossbows','crossbow','Longbows','longbow','Leather Armor','leatherarmor','Metal Armor','metalarmor');
function init()
{
optionTest = true;
lgth = document.forms[0].third.options.length-1;
document.forms[0].third.options[lgth] = null;
if (document.forms[0].third.options[lgth]) optionTest = false;
}
function populate()
{
if (!optionTest) return;
var box2 = document.forms[0].second;
var number = box2.options[box2.selectedIndex].value;
if (!number) return;
var list = store[number];
var box3 = document.forms[0].third;
box3.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box3.options[i/2] = new Option(list[i],list[i+1]);
}
}
</script>
</head>
<body onLoad='init()'>
<h1>Trade goods, weapons and units</h1><p>
I want to:
<form name='tradeselect' method='post' action='trade2.php'>
<select size=8 name='first' width=200>
<option value='buy'>buy</option>
<option value='sell'>sell</option>
<option value='request'>request</option>
</select>
<select size=8 name='second' width=200' onchange='populate()'>
<option value='0'>resources</option>
<option value='1'>troops</option>
<option value='2'>weapons</option>
</select>
namely
<select size=8 name='third' width=200' onchange='submit()'>
<option>Can't initialize!</option>
</select>
</form>
</body>
</html>
When I test this script the normal way, everything is ok, but once I execute this script using php to include this code into another page (and thereby having the <html><head></head><body></body></html> tags etc. twice in one page....), It produces the following error:
'optionTest' is undefined
Now, my question is; How do I get this working, what am I doing wrong?
Thanks in advance! :cool:
I've got a little problem executing a script that triggers an option menu to display different types of items depending on your latest choice...
The script goes as follows:
<html>
<head>
<script language='JavaScript'>
var store = new Array();
store[0] = new Array(
'Gold','gold','Food','food','Wood','wood','Stone','stone','Iron','iron');
store[1] = new Array(
'Irregulars','irregular','Pikemen','pikeman','Swordmen','swordmen','Cavaliers','cavalier','Longsword men',
'longswordman','Archers','archer','Crossbowmen','crossbowman','Longbowmen','longbowman','Fastriders' ,'fastrider');
store[2] = new Array(
'Daggers','dagger','Pikes','pike','Lances','lance','Swords','sword','Longsword','longsword','Bows',' bow',
'Crossbows','crossbow','Longbows','longbow','Leather Armor','leatherarmor','Metal Armor','metalarmor');
function init()
{
optionTest = true;
lgth = document.forms[0].third.options.length-1;
document.forms[0].third.options[lgth] = null;
if (document.forms[0].third.options[lgth]) optionTest = false;
}
function populate()
{
if (!optionTest) return;
var box2 = document.forms[0].second;
var number = box2.options[box2.selectedIndex].value;
if (!number) return;
var list = store[number];
var box3 = document.forms[0].third;
box3.options.length = 0;
for(i=0;i<list.length;i+=2)
{
box3.options[i/2] = new Option(list[i],list[i+1]);
}
}
</script>
</head>
<body onLoad='init()'>
<h1>Trade goods, weapons and units</h1><p>
I want to:
<form name='tradeselect' method='post' action='trade2.php'>
<select size=8 name='first' width=200>
<option value='buy'>buy</option>
<option value='sell'>sell</option>
<option value='request'>request</option>
</select>
<select size=8 name='second' width=200' onchange='populate()'>
<option value='0'>resources</option>
<option value='1'>troops</option>
<option value='2'>weapons</option>
</select>
namely
<select size=8 name='third' width=200' onchange='submit()'>
<option>Can't initialize!</option>
</select>
</form>
</body>
</html>
When I test this script the normal way, everything is ok, but once I execute this script using php to include this code into another page (and thereby having the <html><head></head><body></body></html> tags etc. twice in one page....), It produces the following error:
'optionTest' is undefined
Now, my question is; How do I get this working, what am I doing wrong?
Thanks in advance! :cool: