Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8401589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:50:55+00:00 2026-06-09T21:50:55+00:00

Summary I am making a ordering form that will multiply Qty of Items by

  • 0

Summary

  • I am making a ordering form that will multiply “Qty of Items” by “Unit price” to give the “Subtotal” by JavaScript.
  • The orders are stored in MySQL db and retrieved via PHP.
  • I want to manually enter “Unit Price,” I wish after I type that number in the multiplication will happen.
  • There are 10 rows of items (an array).
  • All written code performs correctly as expected.

Problem

  • My JavaScript function only runs for the first row, not any others.
  • I want to run for every row, for each item type.

My Code

JS

function calc(){
    baseprice=parseInt(document.getElementById("baseprice").value);
    units=parseInt(document.getElementById("units").value);
    x=baseprice*units;
    document.getElementById("sub").value=x;
}

PHP / SQL

$alter=0;
//previous miscellaneous code
while($rows=mysql_fetch_array($sql))
{
    $alter=$alter+1;
 //edit       
$subCalc="<td><input name='units_".$alter."' id='units_".$alter."' type='hidden' value=".$rows['SUM(pizza_orders.qty)']."><input name='baseprice_".$alter."' id='baseprice_".$alter."' type='text' size='2' onchange='calc(this);'></td><td><input name='sub_".$alter."' id='sub_".$alter."' type='text' size='3'></td></tr>";

 //alternates the table row colour by modulus
    if ($alter %2 ==0){
        echo "<tr><td>".$rows['item_order']."</td>";
        echo "<td>".$rows['SUM(pizza_orders.qty)']."</td>".$subCalc;
    }else{
            echo "<tr class=\"alt\"><td>".$rows['item_order']."</td>";
            echo "<td>".$rows['SUM(pizza_orders.qty)']."</td>".$subCalc;
    }
}   

Sample Result / Situation

enter image description here

Extension

Making a grand total at the bottom right, adding all of the sub-totals fields as they are “onchange”.

subTotal=parseInt(document.getElementById("sub" + el.id.replace('baseprice', '')).value);                                                                                   document.getElementById("grandtotal")=  subTotal+parseInt(document.getElementById("grandtotal").value); 

Demo > JS Fiddle

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:50:57+00:00Added an answer on June 9, 2026 at 9:50 pm

    An id has to be unique on a page, you have multiple input’s with the same id on the page. Hence why it is only every performing the calculation on the first input. Your best bet is to append a numerical value to ‘baseprice’ and ‘units’ input to distinguish each row. In addition pass the ‘this’ keyword into the calc function to reference the element that has triggered the function.

    Example row:

        <tr>
        <td>
        <input id="units_1" name="units_1" type="hidden" value="10" />
    <!-- attach an onchange handler to baseprice_1, when a change is made trigger the calc function. -->
        <input id="baseprice_1" name="baseprice_1" type="text" onchange="calc(this);" />
        <input id="sub_1" name="sub_1" type="text" />
        </td>
        </tr>
    

    JS:

    /* An array to store the subtotals. */
    grandTotalArr = [];
    
        function calc(el){
    /* el = this (which is a reference to baseprice_1) */
            baseprice=parseInt(el.value);
            units=parseInt(document.getElementById("units" + el.id.replace('baseprice', '')).value);
            x=baseprice*units;
            document.getElementById("sub" + el.id.replace('baseprice', '')).value=x;
    
    /* Store the subtotal in the grandTotalArr using it's id as the key. */
    grandTotalArr["sub" + el.id.replace('baseprice', '')] = x;
    /* Implode all values with the + operator, and evaluate as a JS expression. Once evaluated, update the grand total. */
    document.getElementById('grand_total').value = eval(grandTotalArr.join('+'));
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

SUMMARY: I'm making a free that will only run for 3-5 months or more.
I am making web page that will sequentially plays video from server on big
Quick summary: I'm making an application that parses a binary file, stores vertices and
I'm making a windows form application that uses threads, the threads is running and
Summary: DataAnnotation's automatic handling of an int? is making me rethink using them at
I have objects that have comments. As part of a periodic email summary, I
So, I'm making a 3rd person game in XNA. I've created a camera that
I hope I can explain this properly. I'm making an android app that, when
I've just finished making this recursive method: /// <summary> /// Recursively process a given
git shortlog is handy for making a human-readable summary of changes. However, when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.