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:)
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:)
<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.txtText Box3.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.
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.txtText
// 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.
ASKER
Hey there...
I will try this and see what happens
Denise:)
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
%>
<%
dim strAll
strAll = Request.Form("txt1") & Request.Form("txt2") ' <--if form method = post
strAll = Request.Querystring("txt1"
%>
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:)
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...
justinbillig's example above should do what you want. My examples were just combining, not adding...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<!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.val
}
//-->
</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>