...

View Full Version : Asp checkboxes and Javascript



ClueLess
09-17-2007, 07:16 PM
I am not sure this post should be in this forum or javascript...since it's asp and javascript so I post it here...

I’ve a list of checkboxes:

Grade 1, Grade 2, Grade 3, Grade 4, Grade 5, Grade 6....

I retrieve the values from the database: if it has a value then display a selected checkbox, if not display just a checkbox – depends on the condition.

The problem I’m having now is if I uncheck the selected checkbox(es) , hit submit button…it still lets me go through.

What I try to do is if I don’t select any checkbox(es) (or uncheck the selected checkbox) - then hit a Submit button.... the alert message will popup and should not let me submit it. How can we do that?

Thanks you very much for your help..

Below is the code I've tried:

<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(formUpdate) {
if (
formUpdate.Grade1.checked == false &&
formUpdate.Grade2.checked == false &&
formUpdate.Grade3.checked == false &&
formUpdate.Grade4.checked == false &&
formUpdate.Grade5.checked == false &&
formUpdate.Grade6.checked == false &&

{
alert ('Please select one of the checkboxes');
return false;
} else {
return true;
}
}
//-->
</script>
</head>

<form name="formUpdate" action="Updated.asp" method="post" onsubmit="return checkCheckBoxes(formUpdate);">
<table>
<tr>
<%if RS("Grade") = "1" then %>
<td colspan="2">
<input type="checkbox" checked name="Grade1" value="cg">Grade 1
</td>
<%else %>
<td colspan="2">
<p><input type="checkbox" name=" Grade1" value="1"> Grade 1
</td>
<%end if %>
</tr>
<tr>
<%if RS("Grade") ="2" then %>
<td colspan="2">
<p><input type="checkbox" checked name="Grade2" value="cg">Grade 2
</td>
<%else %>
<td colspan="2">
<p><input type="checkbox" name="Grade2" value="2">Grade 2
</td>
<%end if %>
</tr>
…so on to Grade 6
<td valign="top"><p>&nbsp;</p></td>
<td valign="top">
<input type="submit" value="Submit" name="submit">
</td>
</tr>
</table>
</form>

nikkiH
09-17-2007, 07:29 PM
Aside from having too much code for something like this, don't name a variable the same as the form. Name conflicts occur. Either name your form something else, or pass it as something else, or don't pass it at all.

Your code would look much nicer like this.
(and it won't get featured on WTF like this http://worsethanfailure.com/Articles/Only-0001-per-Line.aspx)



<tr>
<td colspan="2">
<input type="checkbox" name="Grade1" value="cg"
<%if RS("Grade") = "1" then response.write " checked" end if%>
>Grade 1
</td>
</tr>

Daemonspyre
09-17-2007, 08:19 PM
Another way of making your life easier is to set the Checkboxes inside a loop:


<form name="formUpdate" action="Updated.asp" method="post" onsubmit="return checkCheckBoxes(this);">
<table>
<% for x=1 to 6 %>
<tr><td colspan="2"><input type="checkbox" name="Grade<%=x%>" value="cg" <% if rs("Grade") = x then response.write("checked")%>>Grade <%= x %></td></tr>
<% next %>
<tr>
<td valign="top"><p>&nbsp;</p></td>
<td valign="top"><input type="submit" value="Submit" name="submit"></td>
</tr>
</table>
</form>


I can't help with your JS without completely re-writing your code. There are a lot easier ways to check the 'checked' status of a checkbox without defining each individual unit.

ClueLess
09-17-2007, 08:38 PM
Thanks for reply and suggestions....
The reason I cannot loop because....they want to use the word "sophomore, junior, senior "

also they want to insert different value into the db.... depends on which one that user's selected;

<%if RS("Grade") = "1" then %>

<input type="checkbox" checked name="Grade1" value="cg">Grade 1

<%else %>

<input type="checkbox" name=" Grade1" value="1"> Grade 1

<%end if %>

Daemonspyre
09-17-2007, 09:05 PM
You can still loop while using those words in your text.

You can use SELECT CASE or just a simple IF...THEN...ELSEIF...THEN...END IF statement.

For example:



for loop...
'
SELECT CASE rs("Grade")
Case = "1" txtGrade = "Freshman"
Case = "2" txtGrade = "Sophomore"
END SELECT
'
next
'
' OR
'
for loop...
'
if rs("Grade") = "1" then
txtGrade = "Freshman"
elseif rs("Grade") = "2" then
txtGrade = "Sophomore"
end if
'
next


"They" are correct in that you should be using numerical values for your 'checkboxes', as numbers are more efficient to store in the database than text.

Also, by setting all the checkboxes the same name, you can then use JS to check to see if they are all 'checked' or not with a lot less code...

ClueLess
09-17-2007, 09:35 PM
Thank you very much Daemonspyre....
I am still pulling my hair out....what you mentioned on the previous post is too advance. Is the away to fix this existing code without rewrite it?

nikkiH
09-17-2007, 10:02 PM
Thanks for reply and suggestions....
The reason I cannot loop because....they want to use the word "sophomore, junior, senior "

also they want to insert different value into the db.... depends on which one that user's selected;

<%if RS("Grade") = "1" then %>

<input type="checkbox" checked name="Grade1" value="cg">Grade 1

<%else %>

<input type="checkbox" name=" Grade1" value="1"> Grade 1

<%end if %>

Am I slow today, or does that not make a lot of sense?
I could just be slow, it happens.

Daemonspyre
09-18-2007, 02:53 PM
No, Nikki, you aren't slow. That way is a good way, but it's not 'great'. Great would mean as little manual rewriting as possible and let the database/web server do as much work as possible.


<% for x=1 to 6 %>
<input type="checkbox" name="Grade1" <% if rs("Grade") = "1" then response.write("value=""cg"" checked") else response.write("value=""1""") %> />Grade 1<br />
<% next %>


Fewer lines of code.

As to my post being too advanced, I understand. Just look at this below before you go nutz. :)



<% for x=1 to 6
'
' Make this upper limit 12 if you want to go to US Matriculation, Standard 10 is EU Matric.
'
%>
<input type="checkbox" name="Grade<%= x %>" <% if rs("Grade") = x then response.write("value=""cg"" checked") else response.write("value=""" & x & """") %> />Grade <% = x %><br />
<% next %>

OR

<% for x=1 to 12
'
' Make this upper limit 12 if you want to go to US Matriculation, Standard 10 is EU Matric.
'
SELECT CASE rs("Grade")
Case "1" txtGrade = "First Grade"
Case "2" txtGrade = "Second Grade"
Case "3" txtGrade = "Third Grade"
Case "4" txtGrade = "Fourth Grade"
Case "5" txtGrade = "Fifth Grade"
Case "6" txtGrade = "Sixth Grade"
Case "7" txtGrade = "Seventh Grade"
Case "8" txtGrade = "Eigth Grade"
Case "9" txtGrade = "Freshman"
Case "10" txtGrade = "Sophomore"
Case "11" txtGrade = "Junior"
Case "12" txtGrade = "Senior"
END SELECT
%>
<input type="checkbox" name="Grade<%= x %>" <% if rs("Grade") = x then response.write("value=""cg"" checked") else response.write("value=""" & x & """") %> /><%= txtGrade %><br />
<% next %>



Now, this code works for me, but I do not know how the rest of your code is, so I am not quite sure how it would fit in. But, at least you can see that your entire form page that you posted before can be re-written to be less than half that size.

Let me know if you have any other questions.

nikkiH
09-18-2007, 03:12 PM
I meant logically it seemed odd. If it's pre-selected, the value is cg, otherwise it's a 1?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum