Forum Moderators: open

Message Too Old, No Replies

using dynamic php and sql = javascript T T

txtlval[] + cost[] = valtotal[]

         

nanat

4:02 am on Jul 20, 2009 (gmt 0)

10+ Year Member



how can u add txtlval[] + cost[]= valtotal[]

using Javascript or dom element.. T_T


function suppercase()
{

$result = mssql_query("select * from LevelOne where f0_SubGroupId='33' and IsActive='Y'");
$exist = mssql_num_rows($result);

if($exist > 0)
{
$number = 1;
for ($i = 0; $i < $exist; $i++)
{
$GID = mssql_result($result, $i, "m0_LeveloneId");
$SubGDes = mssql_result($result, $i, "LeveloneDescription");
$IsActive = mssql_result($result, $i, "IsActive");
$unit = mssql_result($result, $i , "Unit");

echo '
<tr>

<td nowrap="nowrap">
<input type="hidden" name="txtlvlno[]" value="'.$GID.'"/>
'.$number.' '.$SubGDes.'</td>
<td><input type="text" size="6" name="cost[]" id="cost[]"></td>
<input type="hidden" name="txtactive[]" value="'.$IsActive.'">
<td>'.$unit.'</td>
<td>Qty/Cost</td>
<td><input type="text" size="6" name="valtotal[]"Total of txtlval[] id="valtotal[]"></td>';

$pd=13;
$ctr=1;
for($t = 0; $t < $pd; $t++)
{

echo '
<td><input type="text" size="6" name="txtlval[]" id="txtlval[]">
<input type="hidden" name="txtlv1id[]" value="'.$GID.'">
<input type="hidden" name="txtpd[]" value="'.$ctr.'">
<input type="hidden" name="txtsubId[]" value="'.$SubGDes.'" />

</td>';

}

$number++;
}

echo '</tr>';

}
else
{
echo 'Access Denied';

}

}

daveVK :bow: Gracias :D

daveVk

1:16 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So each row has input text fields for

Cost, valtotal and 13 txtlval's

and you want valtotal to be sum of all others in row ?
and you want it calculated if any of the other row value change ?
and user should not be able to change valtotal directly ?

nanat

12:30 am on Jul 21, 2009 (gmt 0)

10+ Year Member



yes dave.. each row has input 13 txtlval's and 7 column cost..
<input type="text" size="6" name="txtlval[]" id="txtlval[]" onchange="upperCase()">

i want valtotal the sam of each row.. i mean

txtlval1[] to n * cost = valtotal[]

yes sir dave.. just like my previous post using onchange each entry of txtlval[] will at the same time the valtotal[] is automatically change also..

i already finish my script without onchange and JavaScript.. i just want just to see this is possible.. tnx dave :D

daveVk

12:52 pm on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



function sumRow( el ) {
var inEl, x
var totEl = null;
var sum = 0;
var trEl = el;
while ( trEl.tagName !== 'TR' ) { trEl = trEl.parentNode; }
var inEls = trEl.getElementsByTagName( 'INPUT' );
for ( var i=0; (inEl=inEls[i++]); ) {
__if(inEl.name==='valtotal[]'){ totEl = inEl; }
__if((inEl.name==='cost[]')¦¦(inEl.name==='txtlval[]')){
____x=parseFloat(inEl.value);
____if ( ! isNaN(x)) {sum += x;}
__}
totEl.value = sum;
}

onchange=sumRow(this)

remove all id=

replace _ with space, ¦ with vertical bar

nanat

1:13 am on Jul 22, 2009 (gmt 0)

10+ Year Member



dave what do u mean id="cost[]", id="valtotal[]", id="txtlval[]",

replace by id="¦", id="¦", id="¦"?

daveVk

4:00 am on Jul 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Delete id's completely

eg

<input type="text" size="6" name="txtlval[]" id="txtlval[]" >

becomes

<input type="text" size="6" name="txtlval[]" >

It is illegal to use the same id multiple times, as in multiple rows. I can not see any reason for them, unless there is other javascript that needs them.

nanat

4:54 am on Jul 22, 2009 (gmt 0)

10+ Year Member



ic.. tnx dave.. muchas gracias ^^

nanat

7:56 am on Aug 6, 2009 (gmt 0)

10+ Year Member



dave i try it again dis code but it doesnt working anymore.. what is wrong with my code
my Dom Code..

<script type="text/javascript">
function sumRow(el)
{
var inEl, x
var totEl = null;
var sum = 0;
var trEl = el;
while ( trEl.tagName !== 'TR' )
{
trEl = trEl.parentNode;
}
var inEls = trEl.getElementsByTagName( 'INPUT' );
for ( var i=0; (inEl=inEls[i++]); )
{
if(inEl.name==='txtprice[]')
{
totEl = inEl;
}
if((inEl.name==='txtdos1[]')¦¦(inEl.name==='txtsolution[]'))
{
x=parseFloat(inEl.value);
if ( ! isNaN(x))
{
sum += x;
}
}
totEl.value = sum;
}
}
</script>

<?
$str2="select * from LevelThree
where f0_LevelTwoId='$m0_LeveloneId' and IsInsertable='Y'";
$rs2=$conn->execute($str2);

if (!$rs2->EOF)
{
while (!$rs2->EOF)
{

$m0_LevelTwoId=$rs2->fields("m0_LevelThreeId");
$f0_LeveloneId=$rs2->fields("f0_LevelTwoId");
$LevelTwoDescription=$rs2->fields("LevelThreeDescrition");

$m0_LevelTwoId=$m0_LevelTwoId->value;
$f0_LeveloneId=$f0_LeveloneId->value;
$LevelTwoDescription=$LevelTwoDescription->value;

?>
<td colspan="2" nowrap="nowrap" bgcolor="<?=$color?>">
<input type="text" name="txtvalue[]" size="8" onKeyPress="return keyCheck(event, this)" />
<input type="hidden" name="txtleveltwo[]" value="<?=$m0_LevelTwoId?>" />
<input type="hidden" name="Levelone[]" value="<?=$f0_LeveloneId?>" />

</td>
<td bgcolor="<?=$color?>" nowrap="nowrap"><input type="text" name="txtdos1[]" size="8" onkeypress="return keyCheck(event, this)" />&nbsp;<?=$uom?></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtdos2[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtsolution[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtother[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtallow[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtprice[]" size="8" onkeypress="return keyCheck(event, this)" readonly="readonly" onchange="sumRow(this)"/></td>


<?
$rs2->movenext;
}
}
?>

my php code..


<?
$str2="select * from LevelThree
where f0_LevelTwoId='$m0_LeveloneId' and IsInsertable='Y'";
$rs2=$conn->execute($str2);

if (!$rs2->EOF)
{
while (!$rs2->EOF)
{

$m0_LevelTwoId=$rs2->fields("m0_LevelThreeId");
$f0_LeveloneId=$rs2->fields("f0_LevelTwoId");
$LevelTwoDescription=$rs2->fields("LevelThreeDescrition");

$m0_LevelTwoId=$m0_LevelTwoId->value;
$f0_LeveloneId=$f0_LeveloneId->value;
$LevelTwoDescription=$LevelTwoDescription->value;

?>
<td colspan="2" nowrap="nowrap" bgcolor="<?=$color?>">
<input type="text" name="txtvalue[]" size="8" onKeyPress="return keyCheck(event, this)" />
<input type="hidden" name="txtleveltwo[]" value="<?=$m0_LevelTwoId?>" />
<input type="hidden" name="Levelone[]" value="<?=$f0_LeveloneId?>" />

</td>
<td bgcolor="<?=$color?>" nowrap="nowrap"><input type="text" name="txtdos1[]" size="8" onkeypress="return keyCheck(event, this)" />&nbsp;<?=$uom?></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtdos2[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtsolution[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtother[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtallow[]" size="8" onkeypress="return keyCheck(event, this)" /></td>
<td bgcolor="<?=$color?>"><input type="text" name="txtprice[]" size="8" onkeypress="return keyCheck(event, this)" readonly="readonly" onchange="sumRow(this)"/></td>


<?
$rs2->movenext;
}
}
?>

TNX :D

daveVk

11:57 am on Aug 6, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



onchange="sumRow(this)" needs to be on the inputs that make up the total and not on the total itself. It means if this input is changed calculate total.