Link to home
Start Free TrialLog in
Avatar of neicy62
neicy62

asked on

How to add several textboxes together

Hey there...


I am using asp and vb script.  I am fairly new to this and want to know how to add several values together that are typed into textboxes.  I than want to put the total value in another textbox.

Example

totalvalue = textbox1 + textbox2+ textbox3 + textbox4

Thanks for the help

Denise:)
Avatar of SquareHead
SquareHead

One way:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
      <title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
function combine(){
      document.myForm.txtAll.value = document.myForm.txt1.value + document.myForm.txt2.value;
}
//-->
</script>

</head>

<body>
<form action="" name="myForm" id="myForm">
<input type="text" name="txt1"><br>
<input type="text" name="txt2"><br>
<input type="text" name="txtAll"><br>
<input type="button" value="Combine" onclick="combine();">



</form>


</body>
</html>
<script language="javascript">

function Add( )
{
      // Get the values from the three different text boxes
      var sngValue1 = parseFloat( frmMain.txtTextBox1.value );
      var sngValue2 = parseFloat( frmMain.txtTextBox2.value );
      var sngValue3 = parseFloat(frmMain.txtTextBox3.value );

      // Add them all together and put them in the total box
      frmMain.txtTotal.value = ( sngValue1 + sngValue2 + sngValue3 );
}


</script>
<html>
      <body>
            <form name="frmMain">
                  <input type="text" name="txtTextBox1">
                  <input type="text" name="txtTextBox2">
                  <input type="text" name="txtTextBox3">
            
                  <br>
                  <input type="text" name="txtTotal">
                  <input type="button" value="Add" onclick="Add( );">
            </form>
      </body>
</html>



what your trying to accomplish is generally accomplished with javascript. You have three text boxes and you want to add the values together and put them in another text box. I wrote up a quick example right here. If you type in non numeric characters through you won't get a value.
Avatar of neicy62

ASKER

Hey there...

I will try this and see what happens


Denise:)
Another way, using asp and vbscript would be to submit the form, and handle the concatenation on the action page... Like this:
<%
dim strAll

strAll = Request.Form("txt1") & Request.Form("txt2") ' <--if form method = post

strAll = Request.Querystring("txt1") & Request.Querystring("txt2") ' <--if form method = get or undefined

%>
Avatar of neicy62

ASKER

Hey there

I tried the concatenation method , if I type in 8 + 8 it gives me 88.  How come it is not adding them together per say.  I will try the other 2 methods


Denise:)
Whoops, sorry, I misunderstood.

justinbillig's example above should do what you want. My examples were just combining, not adding...
ASKER CERTIFIED SOLUTION
Avatar of SquareHead
SquareHead

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial