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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the grand total from user input

Status
Not open for further replies.

mbwmbk

Programmer
Dec 12, 2002
19
0
0
US
I have generated a list of items from the table with unit cost.
item# UnitCsot Qty subtotal
item1 2.00 user input javascript to calculate
item2 0.30 user input
.
.
.
item 10 3.15 user input ....

Is there a way to dynamically calculate the grand total from the subtotal input boxes?
if the user change the qty, the grand total will be updated.
 
yes you can do it with Java Script

I suppose you generate your field names like this:

<input type=&quot;text&quot; name=&quot;stockID_<% =rs(&quot;stockID&quot;)%>&quot; value=&quot;<% =rs(&quot;quantity&quot;)%>&quot; size=&quot;4&quot; maxlength=&quot;4&quot;>

and then you can use JS to access all elements like this:

curValue = eval(&quot;document.YOUR_FORM_NAME.stockID_&quot; + curID + &quot;.value&quot;);

this is a bit high level ... but it should point you in the right direction

I hope this helps

I have couple of examples but they are very complex with lot's of other stuff ... but I am a bit busy now to extract it to a simple demo

Sergei
 
I did exactly that with javascript. What you have to keep track of is what element in the form you are at. I did that with the tabindex. Make the table as repetitive as possible so that you can simplify the formulas to work for any row.

I have three functions, updateLineTotal, updateSubTotal, updateGrandTotal. I fire all three for Qty and Unit Price so I can keep track of what function is doing what. I guess I could put them all into one big function...

updateLineTotal is passed the tabindex of the box to be changed as that relates to the position of that box in the form.elements array. I keep tabindex as a variable and write it dynamically via vbscript so that I have access to it when calling the javascript. So, since Qty is two elements before the line item textbox (which is readonly):

Response.Write &quot;<tr><td><input type=text onChange=updateLineTotal(&quot;&tabi+2&&quot;);updateSubttl();updateGrand(); name=qty&quot;&c2&&quot; tabindex=&quot;&tabi&&quot; value=&quot;&objRS(&quot;Qty&quot;)&&quot;></td>&quot;
tabi=tabi+1

c2 is what row I am in and is used to store the values in the database row by row.

function updateLineTotal(target){
var newtarget,tmp,tmp2;
newtarget=parseInt(target,10);
newtarget=(3*newtarget-14)/2;
tmp=parseFloat(pickform.elements[newtarget-1].value,10);
tmp2=parseFloat(pickform.elements[newtarget-2].value,10);
if(tmp<=0 || tmp2<=0){
alert(&quot;Only positive values allowed.&quot;);
}
else{
if(isNaN(tmp) || isNaN(tmp2)){
alert(&quot;Please enter a numeric value.&quot;);
}
else{
pickform.elements[newtarget].value=(tmp*tmp2);
}
}
}

The formula for newtarget translates the tabindex to the element in the form. You will have to modify it to fit your form, possibly. That is why it is important to have a pattern. newtarget-1 is the Unit Price and newtarget-2 is the Qty.

function updateSubttl(){
var newsub;
newsub=0;
for(i=17;i<pickform.length;i+=3){
if(pickform.elements.name.substring(0,6)==&quot;linttl&quot;){
newsub+=parseFloat(pickform.elements.value,10);
}
}
pickform.subttl.value=numsub;
}

I start at the known first line item total on the form and work my way through them until I reach the end. If the name starts with &quot;linttl&quot; then I keep a running total of all the line item totals to get the new subtotal.

function updateGrand(target){
var newttl=parseFloat(pickform.subttl.value,10)
//i had shipping, etc. here which gets added to newttl
pickform.total.value=newttl;
}

To see what element each box is use this:

for(i=0;i<pickform.length;i++){
pickform.elements.value=i;
}

That will help initially to see where you are in the form.
 
Thanks a lot.
I have figure out a way (I got this idea from one of the thread in this forum)to do this. Not sure will it work fine, so far so good. Let me know if this is not the right way to do it.

function calsubtotal(cnt,qtyv,up)
{
var val2= 'subtot'+cnt;
var subtot;
function calsubtotal(cnt,qtyv,up)
{
var val2= 'subtot'+cnt;
var subtot;

if (!checklength(qtyv))
{
qtyv=0;
}
subtot = parseFloat(qtyv)*parseFloat(up);
document.dform.all[val2].value =subtot;


gothru(); // to calculate the grand total
}


function gothru()
{
var e = document.dform.elements;
var tot=0;
var i;
for (i=0; i < e.length; i++)
{
// scan all form elements to find ones we need
var n=document.dform.elements.name;
var v=document.dform.elements.value;

// I named the subtotal input box as subtot&quot;&cnt&&quot;
if (n.substr(0,6) == &quot;subtot&quot; && v.length != 0)
{
if (0 < v)
{
// add another value to tot variable
tot += parseFloat(v,10)
}
}


}
document.dform.tot.value=tot

}

.
.
cnt=0
Do Until
cnt=cnt+1
Response.Write &quot;<td width=50><input name=qty&quot;&cnt&&quot; id=qty&quot;&cnt&&quot; size=1 onchange=&quot;&quot;calsubtotal(&quot;&cnt&&quot;,qty&quot;&cnt&&quot;.value,&quot;&ucost&&quot;)&quot;&quot; ></td>&quot;&vbcrlf
Response.Write &quot;<td width=80><input id=subtot&quot;&cnt&&quot; name=subtot&quot;&cnt&&quot; size=3></td>&quot;& vbCrLf
Loop

Response.Write &quot;<td><input id=tot name=tot size=5 value=0>&quot;& vbCrLf
.
.
.
 
Hey, if it works for you go with it :) My script is a little novice and unsimplified but it gets the job done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top