Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Calculate many textfield

1. ## Calculate many textfield

anyone would you help me

I have some problem to calculate
PHP Code:
``` <table width="415" cellspacing="1" cellpadding="1">     <tr>       <th width="151" scope="col">Price</th>       <th width="189" scope="col">qty</th>       <th width="63" scope="col">total</th>     </tr>     <tr>       <td><label>         <input name="price" type="text" id="price" value="2000">       </label></td>       <td><label>         <input name="qty" type="text" id="qty" value="2">       </label></td>       <td><label>         <input name="total" type="text" id="total" value="4000">       </label></td>     </tr>     <tr>       <td><input name="price" type="text" id="price" value="2000"></td>       <td><input name="qty" type="text" id="qty" value="4"></td>       <td><input name="total" type="text" id="total" value="8000"></td>     </tr>     <tr>       <td>&nbsp;</td>       <td>Total</td>       <td><label>         <input name="gtotal" type="text" id="gtotal" value="12000">       </label></td>     </tr>   </table>  ```
How to calucate it from price * qty = Total and gtotal=sum of total, in the first rows.
Ican do it if there is one row, but i dont know how to calculate samae name of text field like in 2nd or more rows

• You may not have duplicate ids such as price. Give each element a unique id such as price1 etc. Then loop through:

Code:
```var gtot = 0;
for (var i =1; i<= numberofrows; i++) {
var p = "price" + i;
var q = "qty" + i;
var t = "total" + i;
var prc = document.getElementById(p).value;
var quant = document.getElementById(q).value;
var tot = prc * quant;
document.getElementById(t).value = tot;
gtot += tot;
}
document.getElementById("gtotal").value = gtot;
gtot = 0;  // reset grand total```

Quizmaster: How many FA Cup Final appearances has Ashley Cole made? Is it three or six?
Contestant: Four.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•