Hi, I am having troubles validating the value of a form selectoption element that have been populated with a query from a database, all the values show in the combobox, but when I try to validate the user's choice in a Javascript function the value does not show, I even display the value property on the selectoption when it is inside of the fuction and it shows "undefined". Would please somebody help me, I have been looking and I can not find the answer, here is a copy of the script and the form.
Thanks,
------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
function checkfields() {
var statevalue = "";
var stateindex="";
missinginfo = "";
<!-- -------------------------------------------- -->
if (document.form1.first_name.value == "") {
missinginfo += "\n - First Name";
}
<!-- -------------------------------------------- -->
if (document.form1.last_name.value == "") {
missinginfo += "\n - Last Name";
}
<!-- -------------------------------------------- -->
if (document.form1.address.value == "") {
missinginfo += "\n - Address Name";
}
<!-- -------------------------------------------- -->
if (document.form1.city.value == "") {
missinginfo += "\n - City Name";
}
<!-- -------------------------------------------- -->
if (document.form1.state.value == "") {
stateindex = document.form1.state.selectedindex;
missinginfo += "\n - State" + "-" + statevalue + "-" + stateindex;
}
<!-- -------------------------------------------- -->
if (document.form1.zip.value == "") {
missinginfo += "\n - Zip Code";
}
<!-- -------------------------------------------- -->
if (document.form1.country.selectedindex <= 0) {
missinginfo += "\n - Country";
}
<!-- -------------------------------------------- -->
if (document.form1.home_phone.value == "") {
missinginfo += "\n - Home Phone Number";
}
<!-- -------------------------------------------- -->
if ((document.form1.email1.value == "") ||
(document.form1.email1.value.indexOf('@') == -1) ||
(document.form1.email1.value.indexOf('.') == -1)) {
missinginfo += "\n - Email address";
}
<!-- -------------------------------------------- -->
if (document.form1.boats.selectedindex <= 0) {
missinginfo += "\n - Select a Boat";
}
<!-- -------------------------------------------- -->
if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
</SCRIPT>
--------------------------------------------------
<body bgcolor="#FFFFFF">
<table width="550" border="0" align="center">
<form name="form1" onsubmit="return checkfields();" >
<pre>
<tr>
<td bgcolor="#2D862D" width="211"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><font color="#FFFFFF">State</font></b></font></td>
<td width="283" bgcolor="eeeeee">
<select name="state">
<?
$dbh=mysql_connect ("localhost", "--------", "--------") or die ('I cannot connect to the server because: ' . mysql_error());
mysql_select_db ("-------", $dbh) or die ('I cannot connect to the database');
$result = mysql_query("SELECT distinct Initial FROM State order by Initial") or die ("Could not make query.");
//grab all the content
if(($r=mysql_fetch_array($result)) == FALSE)
{
echo "<option>No states.</option>";
}
else
{
do
{
$state=$r["Initial"];
echo "<option>$state</option>";
}while($r=mysql_fetch_array($result));
}
mysql_close($dbh);
?>
</select>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FF0000">* Required</font> </td>
</tr>
<tr valign="bottom" bgcolor="#FFFFFF">
<td colspan="2" height="19" align="center">
<div align="right"> <font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<input type="submit" name="submit" value="Submit Now">
</font></b></font></div>
</td>
</tr>
</pre>
</form>
</table>
</body>
Thanks,
------------------------------------------------------
<SCRIPT LANGUAGE="JavaScript">
function checkfields() {
var statevalue = "";
var stateindex="";
missinginfo = "";
<!-- -------------------------------------------- -->
if (document.form1.first_name.value == "") {
missinginfo += "\n - First Name";
}
<!-- -------------------------------------------- -->
if (document.form1.last_name.value == "") {
missinginfo += "\n - Last Name";
}
<!-- -------------------------------------------- -->
if (document.form1.address.value == "") {
missinginfo += "\n - Address Name";
}
<!-- -------------------------------------------- -->
if (document.form1.city.value == "") {
missinginfo += "\n - City Name";
}
<!-- -------------------------------------------- -->
if (document.form1.state.value == "") {
stateindex = document.form1.state.selectedindex;
missinginfo += "\n - State" + "-" + statevalue + "-" + stateindex;
}
<!-- -------------------------------------------- -->
if (document.form1.zip.value == "") {
missinginfo += "\n - Zip Code";
}
<!-- -------------------------------------------- -->
if (document.form1.country.selectedindex <= 0) {
missinginfo += "\n - Country";
}
<!-- -------------------------------------------- -->
if (document.form1.home_phone.value == "") {
missinginfo += "\n - Home Phone Number";
}
<!-- -------------------------------------------- -->
if ((document.form1.email1.value == "") ||
(document.form1.email1.value.indexOf('@') == -1) ||
(document.form1.email1.value.indexOf('.') == -1)) {
missinginfo += "\n - Email address";
}
<!-- -------------------------------------------- -->
if (document.form1.boats.selectedindex <= 0) {
missinginfo += "\n - Select a Boat";
}
<!-- -------------------------------------------- -->
if (missinginfo != "") {
missinginfo ="_____________________________\n" +
"You failed to correctly fill in your:\n" +
missinginfo + "\n_____________________________" +
"\nPlease re-enter and submit again!";
alert(missinginfo);
return false;
}
else return true;
}
</SCRIPT>
--------------------------------------------------
<body bgcolor="#FFFFFF">
<table width="550" border="0" align="center">
<form name="form1" onsubmit="return checkfields();" >
<pre>
<tr>
<td bgcolor="#2D862D" width="211"><font face="Verdana, Arial, Helvetica, sans-serif" size="2"><b><font color="#FFFFFF">State</font></b></font></td>
<td width="283" bgcolor="eeeeee">
<select name="state">
<?
$dbh=mysql_connect ("localhost", "--------", "--------") or die ('I cannot connect to the server because: ' . mysql_error());
mysql_select_db ("-------", $dbh) or die ('I cannot connect to the database');
$result = mysql_query("SELECT distinct Initial FROM State order by Initial") or die ("Could not make query.");
//grab all the content
if(($r=mysql_fetch_array($result)) == FALSE)
{
echo "<option>No states.</option>";
}
else
{
do
{
$state=$r["Initial"];
echo "<option>$state</option>";
}while($r=mysql_fetch_array($result));
}
mysql_close($dbh);
?>
</select>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif" color="#FF0000">* Required</font> </td>
</tr>
<tr valign="bottom" bgcolor="#FFFFFF">
<td colspan="2" height="19" align="center">
<div align="right"> <font color="#FFFFFF"><b><font face="Verdana, Arial, Helvetica, sans-serif" size="2">
<input type="submit" name="submit" value="Submit Now">
</font></b></font></div>
</td>
</tr>
</pre>
</form>
</table>
</body>