Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate an option element populated with query 1

Status
Not open for further replies.

vibarvi

Programmer
Jan 27, 2009
4
US
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>
 
I don't know PHP, but it looks like you're not setting the value for your option objects.

[tt]echo "<option>$state</option>";[/tt]

should be

[tt]echo "<option value='$state'>$state</option>";[/tt]

Hopefully I've interpreted the syntax correctly.

- Larry
 
vibarvi,

Just a few pointers on posting:

- When posting code, you should really wrap it in [ignore]
Code:
[/ignore] tags to get nicer formatting in a monospaced font (which helps legibility).

- When posting code in a client-side forum, you really should post the client-side code that gets delivered to the browser, rather than the server-side code. In this case, it probably didn't matter, but in most cases, it does.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Larry,

Thank you very much for your reply, it now works, it is so funny because I had this script made with vbscript first and using vbscript you don't need to set the value to the option, it keeps the value from the echo. I had to change to javascript because it was not working with firefox. Thank you so much a was making my head in pieces because I could not see that.

Bill,

Thanks for your advice, I will keep that in mind while writting code.

 
>using vbscript you don't need to set the value to the option, it keeps the value from the echo.
Really? No.
 
tsuji,

Thanks for your comment, may be I am totally wrong but that's is the only explanation that I have about that I could evaluate the content of the selectindex property while using VBscript but I can not do so working with Javascript unless I set the value, or may me I am mix up, but if you know why this happen in vbscript and javascript I would appreciate if you could share it with me.

Thanks again,
 
If you're prepared to appeal only personal experience--and experience is often very deceptive if you don't know what to look for and how--, I have nothing to add but telling the the forum the truth that your statement has no truth in it, since this is a public forum and many innocent eyes are out there.

If you are prepared to re-examine your supposedly existing page in vbs, this is the exact parallellism vbs vs js.

Suppose osel is the reference to the select-one object. The main data are exposed through the following main methods/properties. (i is some index, base 0)
[tt]
vbs: osel.options(i).value
js : osel.options.value

vbs: osel.options(i).text
js : osel.options.text

vbs: osel.options(osel.selectedIndex).value
js : osel.options[osel.selectedIndex].value

vbs: osel.options(osel.selectedIndex).text
js : osel.options[osel.selectedIndex].text

vbs: osel.selectedIndex
js : osel.selectedIndex

vbs: osel.length
js : osel.length

etc etc...
[/tt]
 
tsuji,

You are right, and that is the reason why I enter to this forum, because I was looking for the truth statement that make my script work. Now perhaps you can help me to understand this, I had this function on my prior script:

--------------------------------
Function IsCountry(ByVal FieldName)
If (Document.Form1.country.selectedindex <= 0) Then
x = MsgBox("All Countries is not a valid selection",16,FieldName)
IsCountry = False
Else
IsCountry = True
End If
End Function
-------------------------------------
This is how I set the information in the option from a loop
-------------------------------------
do
{
$country=$r["Name"];
echo "<option>$country</option>";
}while($r=mysql_fetch_array($result));
------------------------------------------------
Why this function works on vbscript in that way, but when you try to recreate the same functionality in Javascript does not work (Values does not show and when I try to see what is in the selectindex property shows as undefined), I know the values are set in the upper function because I save them in a database, so I can see them. Thanks Larry's advice I have a script working with Javascript, but I am still curious about this issue.

Please don't get me wrong I am not pretending that I know a lot when I know that my knowledge is limited, I am just trying to learn and I really appreciate that you are helping me to do not mislead anybody.
 
Why do you say vbs function as shown shows value? where is it shown?

If you add a line attempting to show the value-- delivering your intention--, does it show?!
[tt]
Function IsCountry(ByVal FieldName)
[blue]msgbox document.form1.country.options(0).value
'whatever after: does the above show??? That line is the meaning of showing the value of options(0).[/blue]
If (Document.Form1.country.selectedindex <= 0) Then
x = MsgBox("All Countries is not a valid selection",16,FieldName)
IsCountry = False
Else
IsCountry = True
End If
End Function[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top