...

View Full Version : Reposting with some clarity..hopefully...



arcmax
03-20-2005, 05:29 AM
I have a few dynamically generated text boxes on a php code..

The text box is named amt1, amt2,amt3 , amt4 ...and so on .

What I would like to do is to add(total) the amounts which will be entered into the text boxes ...

I also have hidden field called balance which stores a value.

If the total of the text boxes (amt1+amt2+amt3+...) is less then the value stored within the hidden field called balance then the submit buton should appear otherwise it should not appear.

I got a code from one of the repositories for adding the total fo r a column the only problem is that it add the total of only the first 3 text boxes ...you can see it here..if you can modify the same and make it work to add it for n number of text boxes named amt1..amt2..amt3 and so on., this oculd be used..




<script language="JavaScript">
function cal( amt, t ) {
var len = amt.length;
if( len > 0 && amt.substring( 0, 1 ) == "$" ) {
amt = amt.substring( 1, len );
}
if( parseFloat( amt ) > 0 ) {
if( parseFloat( t ) > 0 ) {
t = parseFloat( t ) + parseFloat( amt );
} else {
t = parseFloat( amt );
}
}
return t;
}

function strip_amt( amt ) {
var amt_char = "";
var new_amt = "";
for( var c=1; c<=amt.length; c++ ) {
amt_char = amt.substring( c-1, c );
if( amt_char == "," ) {
//do nothing
} else {
new_amt = new_amt+amt_char;
}
}
amt = new_amt;
return amt;
}

function calculate_total() {
var t = 0;
var pmt = document.PMTS;
var num = parseInt( pmt.NUM_PAY.value );
var tp
var tp1

for( var i=1; i<=num; i++ ) {
if( i == 1 ) {
if( pmt.NUM1.value == "I" ) {
amt = pmt.AMT1.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
} else if( i == 2 ) {
if( pmt.NUM2.value == "I" ) {
amt = pmt.AMT2.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
} else if( i == 3 ) {
if( pmt.NUM3.value == "I" ) {
amt = pmt.AMT3.value;
amt = strip_amt( amt );
t = cal( amt, t );
}
}
}

t = t * 100;
t = Math.round( t );
t = t / 100;

if( t != pmt.TOTAL.value ) {
if( t == 0 ) {
pmt.TOTAL.value=""
} else {
tp = t * 100;
tp1 = Math.floor( t ) * 100;
if(( tp - tp1 ) == 0 ) {
pmt.TOTAL.value = t + ".00";
} else {
tp1 = Math.floor( t * 10 ) * 10;
if(( tp - tp1 ) == 0 ) {
pmt.TOTAL.value = t + "0";
} else {
pmt.TOTAL.value = t;
}
}
}
}
setTimeout( "calculate_total()", 100 );
}
</script>

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

echo ("<body onload=\"document.PMTS.AMT1.focus()\">\n");
................

Print("<form METHOD=\"POST\" ACTION=\"index2.html\" name=\"PMTS\" id=\"PMTS\">\n");
echo "<input id=NUM_PAY name=NUM_PAY type=hidden value=\"$numrows\">";
For ($i=1;$i<=$numrows;$i++){
echo "<input id=\"NUM$i\" name=\"NUM$i\" type=hidden value=\"I\">";

}

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


While ($row2 = MYSQL_FETCH_ARRAY($r2)){
<input type=\"textbox\" size=\"5\" id=\"AMT$i\" name=\"AMT$i\" size=\"12\" onChange=\"calculate_total()\">
}





Hope this explains what I am trying to do dso.. mind you the above code is only for adding the columns...

Thanks
Apurva M



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum