post the result back to same page??

J

John Davis

<html>
<body>
<Form action="calc.asp" method="post" name="calc">
<P>NUM1: <input type="text" name="num1">
<P>NUM2: <input type="text" name="num2">
<P>RESULT: <input type="text" name="result">
<P><input type="submit">
</Form>

I want to write an ASP page to accept 2 numbers, and return the sum on the
same page. Can we do it in pure ASP without JavaScript?? It sounds an easy
problem, and I did that in ASP.NET and JavaScript, but failed in pure ASP
3.0. Here's the attempts, but definitely not what I want because I want the
result back to the text field.

<%
Dim num1, num2, result
num1 = CInt(Request.Form("num1"))
num2 = CInt(Request.Form("num2"))
result = num1 + num2
Response.Write("<P>Num1 = " & num1)
Response.Write("<P>Num2 = " & num2)
Response.Write("<P>Num3 = " & result)
%>


</body>
</html>
 
R

Ray at home

If you want the result to be in the text field for the result, you have to
write the result into the value of that form element as such.


<%
Dim num1, num2, result
num1 = CInt(Request.Form("num1"))
num2 = CInt(Request.Form("num2"))
result = num1 + num2
%>

<html>
<body>
<Form action="calc.asp" method="post" name="calc">
<P>NUM1: <input type="text" name="num1" value="<%=num1%>">
<P>NUM2: <input type="text" name="num2" value="<%=num2%>">
<P>RESULT: <input type="text" name="result" value="<%=result%>">
<P><input type="submit">
</Form>




</body>
</html>

Ray at home
 
D

dlbjr

<%
dblNum1 = Trim(Request("num1"))
dblNum2 = Trim(Request("num2"))
If IsNUmeric(dblNum1) And IsNumeric(dblNum2) Then
Select Case UCase(Trim(Request("CHOICE")))
Case "ADD"
dblAnswer = CDbl(dblNum1) + CDbl(dblNum2)
Case "SUBTRACT"
dblAnswer = CDbl(dblNum1) - CDbl(dblNum2)
Case "MULTIPLY"
dblAnswer = CDbl(dblNum1) * CDbl(dblNum2)
Case "DIVIDE"
dblAnswer = CDbl(dblNum1) / CDbl(dblNum2)
Case Else
dblAnswer = ""
End Select
End If
%>
<html>
<body>
<form action="addtest.asp" method="post" name="calc" ID="Form1">
NUM1: <input type="text" name="num1" value="<%=dblNum1%>"><br>
NUM2: <input type="text" name="num2" value="<%=dblNum2%>"><br>
RESULT: <input type="text" name="result" value="<%=dblAnswer%>"><br>
<input type="submit" name="CHOICE" value="Add">
<input type="submit" name="CHOICE" value="Subtract">
<input type="submit" name="CHOICE" value="Multiply">
<input type="submit" name="CHOICE" value="Divide">
</form>
</body>
</html>


-dlbjr

invariable unerring alien
 
J

John Davis

yeah, that's what I want, except each text field is filled with 0 before the
user enters any number. Any workarounds on that??
 
K

Ken Schaefer

Just detect if the form has been posted or not.

<%
If UCase(Request.ServerVariables("Request_Method")) = "POST" then

intNum1 = Request.Form("txtNumber1")
intNum2 = Request.Form("txtNumber2")

intTotal = intNum1 + intNum2

Else

intNum1 = 0
intNum2 = 0

End If
%>
<html>
<head>
</head>
<body>
<form method="post">
<input type="text" name="txtNumber1" value="<%=intNum1%>"><br>
<input type="text" name="txtNumber2" value="<%=intNum2%>"><br>
<input type="submit">
</form>

<p>
The total is: <%=intTotal%>
</p>
</body>
</html>




: yeah, that's what I want, except each text field is filled with 0 before
the
: user enters any number. Any workarounds on that??
:
: "Ray at home" <myfirstname at lane 34 . komm> wrote in message
: : > If you want the result to be in the text field for the result, you have
to
: > write the result into the value of that form element as such.
: >
: >
: > <%
: > Dim num1, num2, result
: > num1 = CInt(Request.Form("num1"))
: > num2 = CInt(Request.Form("num2"))
: > result = num1 + num2
: > %>
: >
: > <html>
: > <body>
: > <Form action="calc.asp" method="post" name="calc">
: > <P>NUM1: <input type="text" name="num1" value="<%=num1%>">
: > <P>NUM2: <input type="text" name="num2" value="<%=num2%>">
: > <P>RESULT: <input type="text" name="result" value="<%=result%>">
: > <P><input type="submit">
: > </Form>
: >
: >
: >
: >
: > </body>
: > </html>
: >
: > Ray at home
: >
: >
: >
: >
: > : > > <html>
: > > <body>
: > > <Form action="calc.asp" method="post" name="calc">
: > > <P>NUM1: <input type="text" name="num1">
: > > <P>NUM2: <input type="text" name="num2">
: > > <P>RESULT: <input type="text" name="result">
: > > <P><input type="submit">
: > > </Form>
: > >
: > > I want to write an ASP page to accept 2 numbers, and return the sum on
: the
: > > same page. Can we do it in pure ASP without JavaScript?? It sounds an
: easy
: > > problem, and I did that in ASP.NET and JavaScript, but failed in pure
: ASP
: > > 3.0. Here's the attempts, but definitely not what I want because I
want
: > the
: > > result back to the text field.
: > >
: > > <%
: > > Dim num1, num2, result
: > > num1 = CInt(Request.Form("num1"))
: > > num2 = CInt(Request.Form("num2"))
: > > result = num1 + num2
: > > Response.Write("<P>Num1 = " & num1)
: > > Response.Write("<P>Num2 = " & num2)
: > > Response.Write("<P>Num3 = " & result)
: > > %>
: > >
: > >
: > > </body>
: > > </html>
: > >
: > >
: >
: >
:
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top