Multiple values from checkboxes

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rickky112
    New Member
    • May 2007
    • 4

    Multiple values from checkboxes

    Hello all

    I am very new to asp coding and i have a problem which i need help on.

    Here is the code i've written so far.

    Code:
    <body>
    <%
    year1 = request.form("2007")
    year2 = request.form("2008")
    year3 = request.form("2009")
    year4 = request.form("2010")
    
    	If year1 = "" AND year2 = "" AND year3 = "" AND year4 = "" then
    		endresult = "NO RESULT"
    	End If
    	If year1 = "2007" AND year2 = "" AND year3 = "" AND year4 = "" then
    		endresult = "2007"
    	End If
    	If year1 = "" AND year2 = "2008" AND year3 = "" AND year4 = "" then
    		endresult = "2008"
    	End If
    	If year1 = "" AND year2 = "" AND year3 = "2009" AND year4 = "" then
    		endresult = "2009"
    	End If
    	If year1 = "" AND year2 = "" AND year3 = "" AND year4 = "2010" then
    		endresult = "2010"
    	End If
    
    Response.write endresult
    %>
    <form method="POST" action="checkbox.asp">
    	<p>
    	<input type="checkbox" name="2007" value="2007">2007 
    	<input type="checkbox" name="2008" value="2008">2008 
    	<input type="checkbox" name="2009" value="2009">2009 
    	<input type="checkbox" name="2010" value="2010">2010&nbsp; 
    	<input type="submit" value="Submit" name="B1"></p>
    </form>
    </body>
    What i'm trying to do is when the user ticks the box(s) the value goes into endresult but with a twist!!

    If i do a single tick then endresult would = that value.

    But if i do multiple ticks then i want to find the lowest value then inclued a " - " and find the highest value i.e 2007 - 2009, 2007 - 2010 and so on.

    I've been able to work out single ticks which i have done here. But when if comes to finding a value morethan 1 it becomes a bit more complicated for me at a beginners level.

    I do hope some will be able to help me with this.

    Thanks in advance.

    Rickky
  • rickky112
    New Member
    • May 2007
    • 4

    #2
    I've managed to work this problem out. Here is the code and it might help another newbie like me!!!

    Code:
    <body>
    <%
    yearselected = request.form("year")
    
    If (Len(yearselected)) >= 10 then
    Response.write (Left(yearselected,4)) & " - " & (Right(yearselected,4))
    Else
    Response.write yearselected
    End If
    
    %>
    <form method="POST" action="checkboxnew.asp">
    	<p>
    	<input type="checkbox" name="year" value="2007">2007 
    	<input type="checkbox" name="year" value="2008">2008 
    	<input type="checkbox" name="year" value="2009">2009 
    	<input type="checkbox" name="year" value="2010">2010&nbsp; 
    	<input type="submit" value="Submit" name="B1"></p>
    </form>
    </body>

    Comment

    • viktorka
      New Member
      • Jun 2010
      • 26

      #3
      1. I do not recommend using numbers as a name of form's items. In your case you they have names as "2007". Instead of <input type="checkbox" name="2007" value="2007"> you may use something like <input type="checkbox" name="a2007" value="2007">

      2. Try this code, I I get your what you want:


      <%
      HighestValue = ""
      LovestValue = ""

      For intCount = 2007 to 2010
      If LovestValue = "" then LovestValue = request.form(cS tr(intCount))
      next

      For intCount = 2010 to 2007 step -1
      If HighestValue = "" then HighestValue = request.form(cS tr(intCount))
      next

      if HighestValue = LovestValue then
      endresult = HighestValue
      else
      endresult = LovestValue & " - " & HighestValue
      end if

      response.write( endresult)
      %>

      Comment

      Working...