...

View Full Version : totaling a form ---- URGENT HELP



arcmax
03-20-2005, 06:37 AM
Hi

I got this script from the forum..




<!--

function getVal(){

var doc = document['oFrm'];

doc['tot'].value = ((doc['AMT1'].value)/1) + ((doc['AMT1'].value)/1);
}
}
//-->
</script>

.........

Print("<form METHOD=\"POST\" ACTION=\"index2.html\" name=\"oFrm\">\n");

.......

<input type=\"textbox\" size=\"5\" id=\"AMT$i\" name=\"AMT$i\" size=\"12\" onkeyup=\"javascript:getVal();\" onkeydown=\"javascript:getVal();\">
...
<input id=\"tot\" name=\"tot\" size=\"12\" readonly></font>
</form>



This scvript works for the first two text boxes in the form.

What modification would I need to do so that lets say I have 26 text boxes that need to be totaled...amt1,amt2...amt26...

I know a for loop would be good but I have tried adding a for loop and i keep getitng it worng.. getting things concatinated instead of adding things up...


Need some very urgen help...i know its simple but i think i am not too familiar with the rules of javascriting/c++ hence a poblem implimenting or chnigng the script.

Thanks
Apurva

arcmax
03-20-2005, 07:59 AM
<script language="javascript">
function addAll(){
var frm = document.form;
var len = parseInt(frm.count.value);
var total = 0;
for (var i=1;i<len;i++){
objNum = eval("frm.num"+i);
total+=parseFloat(objNum.value);
}
frm.tot.value=total;
}
</script>

............

Print("<form METHOD=\"POST\" ACTION=\"index2.html\" name=\"f\">\n");
<input type=\"hidden\" name=\"count\" value=\"$numrows\">
<input name=\"tot\" size=\"6\" value=\"00.00\" onfocus=\"this.blur()\" readonly>
While ($row2 = MYSQL_FETCH_ARRAY($r2)){
<input type=\"textbox\" size=\"5\" id=\"Amt\" name=\"AMT$i\" value=\"00.00\" onblur=\"if (isNaN(this.value)) this.value='00.00'\" onchange=\"addAll()\">
}




I got this code from Glenn and well modified it slighly and it seemed to work...
the next problem is how do i check the value in tot against a another value in the form (got from the DB) called Balance and if the value in tot is greater than the value in Balance then the submit button at the end of the form disappears.


Any ideas ??

thanks
Apurva



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum