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 8114579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:11:23+00:00 2026-06-06T03:11:23+00:00

Let us say I have the following script, calculating a sum. Here is the

  • 0

Let us say I have the following script, calculating a sum.

Here is the code:

    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script language="javascript" type="text/javascript">

    function updatesum() {

    line1 = ((document.form.price_1.value -0) * (document.form.number_1.value -0));

    line2 = ((document.form.price_2.value -0) * (document.form.number_2.value -0));

    line3 = ((document.form.price_3.value -0) * (document.form.number_3.value -0));

    line4 = ((document.form.price_4.value -0) * (document.form.number_4.value -0));

    line5 = ((document.form.price_5.value -0) * (document.form.number_5.value -0));

    document.form.sum.value = line1 + line2 + line3 + line4 + line5;

    }
    </script>

    </head>        
    <body>

    <?php

$tR = 1;
$maxlines = 5;

echo '<form name="form" method="post" action="action.php">';
echo '<table><tbody>';

while($tR <= $maxlines)
{
echo '<tr>';
echo '<td width="1"><input name="price_' . $tR . '" onChange="updatesum();"         type="text"></td>';
echo '<td width="1"> x </td>';
echo '<td width="1"><input name="number_' . $tR . '" onChange="updatesum();"         type="text" value="1"></td>';
echo '</tr>';
++$tR;
}
echo '</tbody></table>';

echo 'Total: <input name="sum" value="0" type="text" readonly="readonly">';
echo '<button type="submit" name="Submit" style="display: hidden;">Submit</button>';
echo '</form>';

    ?>

    </body>
    </html>

And that’s fine… But now i change the variable $maxlines from 5 to 500…
How do I adjust the Javascript, without having to write something like:

function updatesum() {

line1 = ((document.form.price_1.value -0) * (document.form.number_1.value -0));

line2 = ((document.form.price_2.value -0) * (document.form.number_2.value -0));

line3 = ((document.form.price_3.value -0) * (document.form.number_3.value -0));

line4 = ((document.form.price_4.value -0) * (document.form.number_4.value -0));

line5 = ((document.form.price_5.value -0) * (document.form.number_5.value -0));

line6 = ((document.form.price_6.value -0) * (document.form.number_6.value -0));

  .......................

line500 = ((document.form.price_500.value -0) * (document.form.number_500.value -0));

document.form.sum.value = line1 + line2 + line3 + line4 + line5 + line6 ........... + line500;

}
  • 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-06T03:11:25+00:00Added an answer on June 6, 2026 at 3:11 am

    You can use a loop to go through all the fields:

    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <script language="javascript" type="text/javascript">
    
            function updatesum() {
                var prices = document.getElementsByName('price');
                var numbers = document.getElementsByName('number');
                var sum = 0;
    
                for (var i = 0; i < prices.length; i++) {
                    sum += prices[i].value * numbers[i].value;
                }
                document.form.sum.value = sum;
            }
            </script>
        </head>        
        <body>
            <?php
                $tR = 1;
                $maxlines = 50;
    
                echo '<form name="form" method="post" action="action.php">';
                echo '<table><tbody>';
    
                while($tR <= $maxlines)
                {
                    echo '<tr>';
                    echo '<td width="1"><input name="price" onChange="updatesum();"         type="text"></td>';
                    echo '<td width="1"> x </td>';
                    echo '<td width="1"><input name="number" onChange="updatesum();"         type="text" value="1"></td>';
                    echo '</tr>';
                    ++$tR;
                }
                echo '</tbody></table>';
    
                echo 'Total: <input name="sum" value="0" type="text" readonly="readonly">';
                echo '<button type="submit" name="Submit" style="display: hidden;">Submit</button>';
                echo '</form>';
            ?>
        </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have the following code: <html> <head></head> <body> <div id=d>some text</div> <script
I have the following html code <!DOCTYPE html> <html lang=en> <head> <meta name=viewport content=width=device-width;
Let's say I have the following, very simple block of code: <script> function hasVal(field)
To simplify my question, let's say I have the following script: var sPath =
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Let's say I have the following code: class MyClass(object): def __init__(self, param): self.param =
Let's say I have the following code, file1 = open(myfile,w) #Write to file1... #Open
Let's say I have a web page ( /index.html ) that contains the following
I'm trying to solve the following problem: Say I have a Python script (let's

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.