Click to See Complete Forum and Search --> : jscript loop for required fields in form help needed.


pepperfraggle
05-04-2003, 10:46 AM
Hello, I'm almost done w/ this program but I'm having trouble w/ a loop in my
admin file. What I have on this page is a bunch of entries to either be
deleted or e-mailed off and then deleted. Having no problem w/ that. In the
admin, the admin chooses which place the e-mail goes but if nothing in the
drop down form menu is choosed and then the admin still chooses to e-mail the
entry it will go nowhere and be deleted. So, I'm trying to make a loop to
make this drop down menu a required field using jscript. I'm testing w/ i<3
but the required fields aren't working. Thanks, and here's a copy of the
jscript. Let me know if you need more.
<script LANGUAGE="Javascript">
<!--
function checkSurvey() {
for(var i=0; i<3; i++){
if (document.me.dept$i.selectedIndex == "") {
alert("Please enter department.")
return false

}
i++;
}

return true

}
// -->
</SCRIPT>

gil davis
05-04-2003, 05:55 PM
Without seeing the HTML for the form, we cannot give you exact answers. However, I can tell you that there are many seemingly incorrect things in your script.
if (document.me.dept$i.selectedIndex == "") {The selectedIndex is a number between -1 and the length of the drop down. It will never be an empty string.

Also, you will not find an object in the DOM using "dept$i" as an index. It should probably be more likedocument.me["dept$" + i].selectedIndexbut without the HTML of the form, I can't be more specific.}
i++;
}This appears wrong, because you are already incrementing i in the for loop statement. This would increment i twice, and skip fields. Unless that is what you intended.

pepperfraggle
05-04-2003, 06:33 PM
actually the default of the drop down has no value

pepperfraggle
05-04-2003, 07:56 PM
print <<"ARF";
<html><head><TITLE>Megaphone Admin</TITLE>

<script LANGUAGE="Javascript">
<!--
function checkSurvey() {
for(var i=0; i<3; i++){
if (document.me.dept0.selectedIndex == "") {
alert("Please enter department.")
return false

}
i++;
}

return true

}
// -->
</SCRIPT>

</head><center><table border="0" cellspacing="1"
width="100%" cellpadding="4">
<tr>

<td width="100%" bgcolor="#C09627"><font face="Arial"
color="#000000"><strong><small>Megaphone Admin
Area</small></strong></font></td>
</tr>
</table>
ARF

print"<form method=post action=me.cgi name=me enctype=multipart/form-data OnSubmit=\"return checkSurvey()\">\n";
print"<input type=hidden name=location value=update_entries>\n";
my $counter=0;
my $count=0;
my $per=0;
while ((my $name, my $email1, my $email2, my $subject, my $choice, my $note, my $id, my $time) = &FetchRow ) {

#do what ever you want to do for each row here

print <<"END";



<table border="0" cellpadding="4" cellspacing="1" width="100%">
<tr> <td width="6" bgcolor="#F3F3F3"><font face="Verdana" size="1">
END
print "<input type=\"radio\" name=\"naction" . $counter . "\" value=\"sendit\"> email<br>";
print "<input type=\"radio\" name=\"naction" . $counter . "\" value=\"deleteit\"> delete<br></font></td>";
$counter++;


print<<"ME";
<td width="613"><font face="Verdana" size="1"><strong>Name:</strong>$name<BR>
<strong>Email:</strong> $email1$email2<br><strong>Subject:</strong>$subject<BR>
ME
print "<strong>Department:</strong> <select name=\"dept" . $count . "\" SIZE=\"1\">";
print<<"YOU";
<option value="">Please choose a department:</option>
<option value="asc\@missouri.edu">Academic Support Center</option>

<option value="asc@missouri.edu">Academic Support Center</option>

<option value="email3@email.com">Main Office</option>

<option value="email2@email.com">Alumni Center</option>
<option value="email3@email.com">Arts & Science</option>

</select><br>
YOU

print <<"FRED";
<strong>Type:</strong> $choice<br>
<strong>Date:</strong> $time<br>
<strong>Message: </strong> $note
</font></td>
</tr>
</table><HR>
FRED

print "<input type=hidden name=\"rowid" . $per . "\" value='$id'>";
$per++;

$count++;
}
print "<input type=hidden name=\"maxcount\" value='$count'>";
print "<input type=submit value=Submit>&nbsp;<INPUT TYPE=reset></form></html>\n";
}
#######################

gil davis
05-05-2003, 06:45 AM
Originally posted by pepperfraggle
actually the default of the drop down has no value The selectedIndex of a SELECT object with no option selected is -1. Once you select an OPTION, the selectedIndex becomes the ordinal assigned to that OPTION, unless the SELECT object is defined as MULTIPLE.

gil davis
05-05-2003, 06:48 AM
Originally posted by pepperfraggle
print <<"ARF";I don't recognize what you have posted as being plain HTML. If you would care to identify what language it is, or the resulting plain HTML, I'll be glad to look at it again.

Or, perhaps someone else knows what that stuff is, and can guide you further.

Scriptage
05-05-2003, 09:20 AM
It's looking pretty much like Perl to me....
Try..
<script LANGUAGE="Javascript">
<!--
function checkSurvey() {
for(var i=0; i<3; i++){
var test = "dept"+i;
if(document.me[test].selectedIndex == 0){
alert("Please select a department.");
return false;
}
}
}
</script>

That should work....

You just tried to mix perl and javascript.

Regards

pepperfraggle
05-06-2003, 02:58 PM
Wow, thanks a whole bunch!

What if I decided to replace 3 w/ some kind of variable that knew how many entries there were?
Thanks!


<script LANGUAGE="Javascript">
<!--
function checkSurvey() {
for(var i=0; i<3; i++){
var test = "dept"+i;
if(document.me[test].selectedIndex == 0){
alert("Please select a department.");
return false;
}
}
}
</script>

Scriptage
05-08-2003, 07:10 AM
Simply replace the 3 with the variable you wish to use, ie:

<script LANGUAGE="Javascript">
<!--
function checkSurvey() {
for(var i=0; i<$yourVariableHere; i++){
var test = "dept"+i;
if(document.me[test].selectedIndex == 0){
alert("Please select a department.");
return false;
}
}
}
</script>

Perl will simply replace the $yourVariableHere with the number $yourVariableHere contains.

Or the above without the $ sign if you are going to define the variable in javascript

Regards
Carl