Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Banned
    Join Date
    Jul 2011
    Location
    htdocs
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    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.
    Last edited by Philip M; 07-22-2011 at 07:05 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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