0

how to sum all those field (name="envelope[]") and display the result in another input. I'm not sure how to do that because those fields are generated dynamycally. tks Seby

UPDATE: here's what I tried with the javascript function. the problem I have is that the function start but gives me the result of all envelope[] input. But i need the result on every lines, not from all the envelope[] fields. I've change the hilighted field, but the top amount changed. see example here: http://www.soniajanelle.ca/example.jpg

   function findTotal(){
    var arr = document.getElementsByName('envelope[]');
    var tot=0;
    for(var i=0;i<arr.length;i++){
        if(parseInt(arr[i].value))
            tot += parseInt(arr[i].value);
    }
    document.getElementById('totalsum').value = tot;
}

</script>
</head>
<body>

<div id="slimScroll" style="overflow:auto; overflow-x:hidden; overflow-y:hidden;  height:<? echo $row_admin_detail['size_height'];?>px;" >


<table width="100%" align="center" border="0">
<tr><td  height="0px" align="center" colspan="2"></td></tr>
<tr>
<table border="0" width="100%" class="ft12" >
<tr><td  class="ann_title" align="center" colspan="2"><h2>Transactions</h2></td></tr>
    <td valign="top" align="center" >
    <div id="delDiv" >
</table>
    <table  width="450px" border="0" cellspacing="0" cellpadding="0" >

<tr class="table" height="125px">
    <th width="1%" align="center"></th> 
    <th width="3%" align="center"></th>
    <th width="10%" align="center" colspan="2" >Action</th> 
    <th width="8%" align="center">Type</th> 
    <th width="8%" align="center">Date</th> 
    <th width="20%" align="center">Payee</th>   
    <th width="8%" align="center">Amount</th>
    <th width="20%" align="center">Envelope</th>
    <th width="2%" align="center"></th>     
<?
    $query = "SELECT * from envelope_sub order by budgeted_amount desc";
    $result = mysql_query($query) or die(mysql_error());
    while ($envelopeSub = mysql_fetch_array($result)) 
    {   
    echo "  
    <th width=\"8%\" align=\"right\" 
    ><p><FONT size=\"2\"> ".$envelopeSub['real_amount']=number_format($envelopeSub['real_amount'],2,'.','')."$</FONT> </p><br>
    <p class=\"rotate\">".$envelopeSub['name']."</p></th>
    ";
    }
?>  

</tr>

<tr >
<form method="post" action="main.php?act=stats&do=add&type=insert">
    <th  align="center"></th>   
    <th  align="center"><img src="http://www.sebyphotographe.com/simplestudio/images/plus-icon 2.png" class="removeimg"></a></th>
    <th  align="center" colspan="2" ><div class="ann_search_submit"><input class="ann_submit" type="submit" value="Ajouter" /></div></th>   
    <th  align="center"><input class="ann_textbox"  name="type" type="text" size="8%"/></th>    
    <th  align="center"><input class="ann_textbox"  name="date" type="text" size="8%"/></th>    
    <th  align="center"><input class="ann_textbox"  name="payee" type="text" size="20%"/></th>
    <th  align="center"><input class="ann_textbox"  name="amount" type="text" size="8%" onkeypress="return isNumeric(event)" /></th>
    <th  align="center"><select>
  <option selected="selected">Distribuer montant...</option>
  <option>par pourcentage</option>
  <option>par montant fixe</option>
  <option>Manuellement</option>
</select></th>  
    <th  align="center"></th>
<?
    $query = "SELECT * from envelope_sub order by id desc";
    $result = mysql_query($query) or die(mysql_error());
    while ($envelopeSub = mysql_fetch_array($result)) 
    {   
    echo "  
    <input type=\"hidden\" name=\"envelope_id[]\" value=\"".$envelopeSub['id']."\">
    <th  align=\"center\"><input class=\"ann_textbox\"  name=\"envelope[]\" type=\"text\" size=\"8%\" onkeypress=\"return isNumeric(event)\" /></th>
    ";
    }
?>


</form> 
</tr>


<?
        $i=0;
        $k=0;
        $transactions = mysql_query("select * from transactions");
        while($row_transactions=mysql_fetch_array($transactions))
        {   
?>


<form method="post" action="main.php?act=stats&do=edit&type=update">
<input type="hidden" name="id" value="<? echo $row_transactions['id']?>">
   <tr class="<?=$bgcolor?>"  nowrap="nowrap" onmouseover="style.backgroundColor='#EAEFF3';" onmouseout="style.backgroundColor='<?=$bgs?>'" >
        <td align="left"  nowrap="nowrap">&nbsp;</td>
        <td colspan="2" ><a href="main.php?act=stats&do=del&id=<? echo $row_transactions['id']?>&type=p" class="remove"><img src="http://www.sebyphotographe.com/simplestudio/images/icon_delete.png" class="removeimg"></a></td>
        <td ><div  class="ann_search_submit"><input class="ann_submit"  type="submit" value="Update" ></div></td>
        <td ><input class="ann_textbox"  name="type" type="text" value="<?echo $row_transactions['type']?>" size="8%"/></td>
        <td ><input class="ann_textbox"  name="date" type="text" value="<?echo $row_transactions['date']?>" size="8%"/></td>
        <td ><input class="ann_textbox"  name="payee" type="text" value="<?echo $row_transactions['payee']?>" size="20%"/></td>
        <?  $row_transactions['amount']=number_format($row_transactions['amount'],2,'.',''); ?>
        <td >
        <? if ($row_transactions['type']=="Expense"){?>                             
        <input id="totalsum" class="ann_textbox" name="amount" type="text" style="color:red;text-align:right;" value=" <?echo "(".$row_transactions['amount'].")";?>" size="8%"/></td><? 
                                                    }ELSE{ ?>
        <input id="totalsum" class="ann_textbox"  name="amount" type="text" style="text-align:right;" value=" <?echo $row_transactions['amount'];?>" size="8%"/></td><? } ?>                                            
        <td ><input class="ann_textbox"  name="envelope_<?echo $i?>" type="text" value="<?echo $row_transactions['envelope']?>" /></td>
        <td></td>   
<?
    $query_env = "SELECT * from transactions_details WHERE transactions_id='".$row_transactions['id']."' order by enveloppe_sub_id  desc";
    $result_env = mysql_query($query_env) or die(mysql_error());
    while ($env_amount = mysql_fetch_array($result_env)) 
    {   
    if ($env_amount['amount']==0){$env_amount['amount']="";}
    $env_amount['amount']=number_format($env_amount['amount'],2,'.','');

    if ($env_amount['type']=="Expense"){
    echo "
    <input type=\"hidden\" name=\"trans_id[]\" value=\"".$env_amount['transactions_id']." \">   
    <input type=\"hidden\" name=\"envelope_id[]\" value=\"".$env_amount['enveloppe_sub_id']."\">
    <input type=\"hidden\" name=\"type_env[]\" value=\"".$row_transactions['type']."\">
    <td  align=\"center\"><input onchange=\"findTotal()\" class=\"ann_textbox\"  name=\"envelope[]\" type=\"text\" size=\"8%\"  style=\"text-align:right;color:red\" value=\"(".$env_amount['amount'].")\" /></td>";
    }ELSE{
    echo "
    <input type=\"hidden\" name=\"trans_id[]\" value=\"".$env_amount['transactions_id']." \">   
    <input type=\"hidden\" name=\"envelope_id[]\" value=\"".$env_amount['enveloppe_sub_id']."\">
    <input type=\"hidden\" name=\"type_env[]\" value=\"".$row_transactions['type']."\">
    <td  align=\"center\"><input onchange=\"findTotal()\" class=\"ann_textbox\"  name=\"envelope[]\" type=\"text\" size=\"8%\"  style=\"text-align:right;\" value=\"".$env_amount['amount']."\" /></td>";
    }
    }

?>

   </tr>
</form>

<? 
$i++;
$k++;
}
?>
2
  • what fields and what have you tried? We need to see your code or some sample code Commented Aug 23, 2013 at 15:40
  • <input class="ann_textbox" name="envelope[]" type="text" size="8%" style="text-align:right;color:red" value="(175.00)" /></td> All of those name="envelope[]" enveloppe field. I did not tried really, cause i did not found on the net what I was looking for exactly. Commented Aug 23, 2013 at 15:45

1 Answer 1

2

Something like this should do the trick

var elems = document.getElementByName('envelope[]');

var sum = 0;
for (var i = 0; i < elems.length; i++)
{
    sum += parseInt(elems[i].value);
}

document.getElementById('yourelementid').value = sum;
Sign up to request clarification or add additional context in comments.

4 Comments

Tks, 2 little questions, how do I paste the result into another input field and how to accomplish that ONCHANGE Tks
@seby: this line document.getElementById('yourelementid').value = sum; assign the result to a different input. In order to do this onchange you'll need to assign a change handler for every input where applies, e.g. <input type="hidden" name="envelope_id[]" value="2" onchange="myfunction();"/>
see my update up there. So I now have a new problem. tks for help.
Hi again everyone. Is there anyone that could help me on that one please. tks seby

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.