Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP Form Calculation

Status
Not open for further replies.

briggsc

Technical User
Jan 1, 2005
14
US

Hi, I've created a form where the form field integer values are added to create and display a total once submitted, but my form is displaying a Mismatch error. Do anyone have any suggestions? Thanks for your help! Here's the code.

<% dim field1, field2, field3
field1 = request.form("field1")
field2 = request.form("field2")
field3 = request.form("field3")
total = field1*1 + field2*1 + field3*1
%>

<form action="untitled2.asp" method="post">
Field 1:&nbsp;&nbsp;<input type="text" name="field1"><BR><BR>
Field 2:&nbsp;&nbsp;<input type="text" name="field2"><BR><BR>
Field 3:&nbsp;&nbsp;<input type="text" name="field3"><BR><BR>
<input type="Submit" value="Submit">
<br><BR><BR>
<%=total%>
</form>
 
This is really one for the ASP forum, but I'll take a stab...


Missing spaces around operators, and multiplying strings isn't defined.

Maybe:

[tt]total = CSng(field1) * 1 + CSng(field2) * 1 + CSng(field3) * 1[/tt]

Hmm, multiplying by 1?

Also, [tt]total[/tt] hasn't been defined and [tt]Option Explicit[/tt] is missing.

Don't let anybody tell you that you don't need [tt]Option Explicit[/tt]. Without it you can end up with some thorny-to-find bugs, and it's free! It totally escapes me why this isn't the default and they even offer you the option.
 
Hi, Thanks for your help. Using the code I still get the type mismatch error. Thanks Again!
 
briggsc,

As a simple simulation of what might happen, run this standalone vbs and you will see how things behave. You can use response.write to reproduce exactly the same effect.
Code:
a=array("123abc", "123.456", "123" & chr(13), "123" & chr(10), _
	"123" & chr(&h20), "123" & chr(&h20) & chr(10), _
	"123" & chr(&h20) & "456", "123" & chr(&h0d) & "456")
for i=0 to ubound(a)
	display a(i)
next

sub display (s)
on error resume next
t=csng(s)
if err<>0 then
	wscript.echo "Runtime error returned"  & vbcrlf & escape(s) & vbcrlf & hex(err.number) & vbcrlf & err.description
else
	wscript.echo "No error returned" & vbcrlf & escape(s) & vbcrlf & t
end if
err.clear
on error goto 0
end sub
- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top