Hello everyone!
I am new to JavaScript and have been using PHP, but needed to use a dynamic form for information about County/State. Here is part of the code (keep in mind I did all 50 states this way-that's well over 3000 counties):
<script language="JavaScript" type="text/javascript">
<!--
//first combo box
data_1 = new Option("Alabama", "$");
data_2 = new Option("Alaska", "$$");
data_3 = new Option("Arizona", "$$$");
//second combo box
data_1_1 = new Option("Autauga", "-");
data_1_2 = new Option("Baldwin", "-");
data_1_3 = new Option("Barbour", "-");
data_1_4 = new Option("Bibb", "-");
data_1_5 = new Option("Blount", "-");
data_1_6 = new Option("Bullock", "-");
data_1_7 = new Option("Butler", "-");
data_1_8 = new Option("Calhoun", "-");
data_1_9 = new Option("Chambers", "-");
data_1_10 = new Option("Cherokee", "-");
data_2_1 = new Option("Aleutians East", "--");
data_2_2 = new Option("Aleutians West", "--");
data_2_3 = new Option("Anchorage", "--");
data_2_4 = new Option("Bethel", "--");
data_2_5 = new Option("Bristol Bay", "--");
data_2_6 = new Option("Denali", "--");
data_2_7 = new Option("Dillingham", "--");
data_2_8 = new Option("Fairbanks North Star", "--");
data_2_9 = new Option("Haines", "--");
data_2_10 = new Option("Juneau", "--");
data_3_1 = new Option ("Apache", "--");
data_3_2 = new Option ("Cochise", "--");
data_3_3 = new Option ("Coconino", "--");
data_3_4 = new Option ("Gila", "--");
data_3_5 = new Option ("Graham", "--");
data_3_6 = new Option ("Greenlee", "--");
data_3_7 = new Option ("La Paz", "--");
data_3_8 = new Option ("Maricopa", "--");
data_3_9 = new Option ("Mohave", "--");
data_3_10 = new Option ("Navajo", "--");
// other parameters
displaywhenempty=""
valuewhenempty=-1
displaywhennotempty="-select-"
valuewhennotempty=0
function change(currentbox) {
numb = currentbox.id.split("_");
currentbox = numb[1];
i=parseInt(currentbox)+1
// I empty all combo boxes following the current one
while ((eval("typeof(document.getElementById(\"combo_"+i+"\"))!='undefined'")) &&
(document.getElementById("combo_"+i)!=null)) {
son = document.getElementById("combo_"+i);
// I empty all options except the first one (it isn't allowed)
for (m=son.options.length-1;m>0;m--) son.options[m]=null;
// I reset the first option
son.options[0]=new Option(displaywhenempty,valuewhenempty)
i=i+1
}
// now I create the string with the "base" name ("stringa"), ie. "data_1_0"
// to which I'll add _0,_1,_2,_3 etc to obtain the name of the combo box to fill
stringa='data'
i=0
while ((eval("typeof(document.getElementById(\"combo_"+i+"\"))!='undefined'")) &&
(document.getElementById("combo_"+i)!=null)) {
eval("stringa=stringa+'_'+document.getElementById(\"combo_"+i+"\").selectedIndex")
if (i==currentbox) break;
i=i+1
}
// filling the "son" combo (if exists)
following=parseInt(currentbox)+1
if ((eval("typeof(document.getElementById(\"combo_"+following+"\"))!='undefined'")) &&
(document.getElementById("combo_"+following)!=null)) {
son = document.getElementById("combo_"+following);
stringa=stringa+"_"
i=0
while ((eval("typeof("+stringa+i+")!='undefined'")) || (i==0)) {
// if there are no options, I empty the first option of the "son" combo
// otherwise I put "-select-" in it
if ((i==0) && eval("typeof("+stringa+"0)=='undefined'"))
if (eval("typeof("+stringa+"1)=='undefined'"))
eval("son.options[0]=new Option(displaywhenempty,valuewhenempty)")
else
eval("son.options[0]=new Option(displaywhennotempty,valuewhennotempty)")
else
eval("son.options["+i+"]=new Option("+stringa+i+".text,"+stringa+i+".value)")
i=i+1
}
//son.focus()
i=1
combostatus=''
cstatus=stringa.split("_")
while (cstatus[i]!=null) {
combostatus=combostatus+cstatus[i]
i=i+1
}
return combostatus;
}
}
//-->
</script>
That works fine, except for one problem. I am trying to put this information in a SESSION using PHP, but it isn't working. Here is that portion of my form code:
<html>
<form action="statecounty.php" method="post">
<TR>
<TD>
<FONT style="FONT-SIZE: 11pt" face=Arial>
*State:
</FONT>
</TD>
<TD>
<select tabIndex="13" name="combo0" id="combo_0" onChange="change(this);" style="width:200px;">
<option value="value1">-select-</option>
<option value="value2">Alabama</option>
<option value="value3">Alaska</option>
<option value="value4">Arizona</option>
</select>
</TD>
<TR>
<TD>
<FONT style="FONT-SIZE: 11pt" face=Arial>
*County:
</FONT>
</TD>
<TD>
<select tabIndex="14" name="combo1" id="combo_1" onChange="change(this)" style="width:200px;">
<option value="value1"> </option>
</select>
</TD>
</TR>
</form>
</html>
And this is located in the "statecounty.php" for that part:
<?php
session_start();
//Form validation:
if((!empty($_POST['combo1']))
&& (strlen($_POST['combo1']) < 30))
{
$county=$_POST['combo1'];
$county=htmlspecialchars($county);
$_SESSION['combo1'] = $county;
}
else
{
$errors[]= 'You forgot to enter a valid entry=County!';
}
if((!empty($_POST['combo0']))
&& (strlen($_POST['combo0']) < 30))
{
$state=$_POST['combo0'];
$state=htmlspecialchars($state);
$_SESSION['state'] = $state;
}
else
{
$errors[]= 'You forgot to enter your State!';
}
if (empty($errors))
{
header
("Location:http://www.thisnextpage.php");
exit();
}
if (!empty($errors) && is_array($errors))
{
echo '<h1>Error!</h1>
The following error(s) occured:<br/ >';
foreach ($errors as $msg)
{
echo " - $msg<br />\n";
}
}
?>
That part works just fine...and now the page that is supposed to pull the information:
<?php
session_start();
echo "{$_SESSION['combo1']}, {$_SESSION['combo0']}\n";
?>
Any ideas on what I'm doing wrong?
Thanks,
~Amy