Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Asp checkboxes and Javascript

    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:
    Code:
    <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>

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    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...-per-Line.aspx)

    Code:
    <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>

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    Another way of making your life easier is to set the Checkboxes inside a loop:

    Code:
    <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.
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #4
    Regular Coder
    Join Date
    Nov 2002
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy

    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 %>

  • #5
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    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:

    Code:
    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...
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #6
    Regular Coder
    Join Date
    Nov 2002
    Posts
    184
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #7
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Quote Originally Posted by ClueLess View Post
    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.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #8
    Regular Coder
    Join Date
    Mar 2007
    Posts
    505
    Thanks
    1
    Thanked 19 Times in 19 Posts
    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.
    Code:
    <% 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.

    Code:
    <% 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.
    To say my fate is not tied to your fate is like saying, 'Your end of the boat is sinking.' -- Hugh Downs
    Please, if you found my post helpful, pay it forward. Go and help someone else today.

  • #9
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    I meant logically it seemed odd. If it's pre-selected, the value is cg, otherwise it's a 1?

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •