Simple form to add numbers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aagase29
    New Member
    • Dec 2009
    • 1

    Simple form to add numbers

    The code below accepts two numbers from textboxes and should show the addition in the third one.I can t figure out how to display addition in the third text box...
    Code:
    <html>
    <body>
    
    <form action="addition_n.asp" method="post">
    First number:&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="no1" size="20" />
    &nbsp;
    <p>
    Second number: <input type="text" name="no2" size="20" value="5" />
    &nbsp;</p>
    <p>
     
     Addition:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="no3" size="20"/>
    </p>
    <p>&nbsp;
    <input type="submit" value="Submit" onclick="call add()" />
    </p>
    
    <%
    sub add()
    dim n1,n2,n3 
    n1=cint(Request.form("no1"))
    n2=cint(Request.form("no2"))
    n3=n1+n2
    Response.write(n3)
    end sub
    
    call add()
    %>
    
    </form>
    
    </body>
    </html>
  • sanjib65
    New Member
    • Nov 2009
    • 102

    #2
    Sorry I can't display it VB.NET but in C# it's done this way

    Code:
    protected void Button1_Click(object sender, EventArgs e)
        {
            int i = 0;
            i = Convert.ToInt32(TextBox1.Text) + Convert.ToInt32(TextBox2.Text);
            TextBox3.Text = i.ToString();
        }

    Comment

    • jhardman
      Recognized Expert Specialist
      • Jan 2007
      • 3405

      #3
      The big problem is that the ASP is executed on the server before it is sent to the browser, but your call is executed when the user clicks the button. The server will never execute the function, because IT ALREADY FINISHED EXECUTING ALL OF THE ASP CODE. If you want it done on the browser, you will need to use a client-side technology, like javascript. Otherwise, try it like this:
      Code:
      <form method="post" action="thisPage.asp">
      <input type="text" name="n1" value="<%=request.form("n1")%>">
      <input type="text" name="n2" value="<%=request.form("n2")%>">
      <%
      dim n3
      n3 = 0
      if request.form("n1") <> "" AND request.form("n2") <> "" then
         n3 = cint(request.form("n1") + request.form("n2")
      end if
      <input type="text" name="n3" value="<%=n3%>">
      <input type="submit" name="submit" value="Add">
      </form>
      this method submits the form back to the server, the server looks it over and refills the form with the original inputs, and performs the addition, before sending the form back to the client. Does this make sense?

      Jared

      Comment

      Working...