Math In ASP - Adding forms

Results 1 to 2 of 2

Thread: Math In ASP - Adding forms

  1. #1
    Join Date
    Dec 1969

    Default Math In ASP - Adding forms

    I have a form, and I want to count the number of checkboxes a user checks. The boxes are named: 1,2,3,4,5(all with a value of one). I want to get the sum of them(i.e if two boxes were check I want the FinalScore to equal two). What am I doing wrong? With the below script, the two numbers are put together(i.e if two boxes were checked, FinalScore equals 11, since each boxes value is one).<BR> <BR> Score = Score & request.form("1")<BR> Score = Score & request.form("2")<BR> Score = Score & request.form("3")<BR> Score = Score & request.form("4")<BR> Score = Score & request.form("5")<BR> FinalScore = Score<BR><BR>Thanks for your help!<BR><BR>- Andrew<BR> <BR>

  2. #2
    Join Date
    Dec 1969

    Default & means string concatenate... you weren&#039;t doing any math, anyway! ASP uses the normal math symbols: + - * / etc.<BR><BR>So you might think you could do:<BR>&nbsp; &nbsp; Score = Request("1") + Request("2") + Request("3") + Request("4") + Request("5")<BR><BR>HOWEVER...<BR><BR>The value of *ALL* is *ALWAYS* a string! And when you use + with a string you get...yep, you got it, another string!<BR><BR>Further, the value of an UNchecked checkbox is *not* zero! It is blank. Non-existent. "".<BR><BR>So you couldn&#039;t even do CINT(Request("2")) because that would give you a type mismatch if the box was unchecked!<BR><BR>ON TOP OF ALL THAT... When you use numbers for the names of fields in form, THEY DO NOT WORK! Instead of getting the field *named* "3", you will get the field *numbered* 3!<BR><BR>You really need to change the names of your fields! I&#039;ll assume you named them "CB1", "CB2", etc.<BR><BR>So here is how you get the score:<BR><BR>&#060;%<BR>Score = 0<BR>For cbnum = 1 TO 5<BR>&nbsp; &nbsp; If Request("CB" & cbnum) &#060;&#062; "" Then Score = Score + 1<BR>Next<BR>%&#062;<BR><BR>You might note, in the process, that we don&#039;t really care what the value of each checkbox is. If it was checked, it *has* a value; if not, it won&#039;t.<BR><BR>Okay?<BR><BR><BR><BR>

Posting Permissions

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