title: Creating a loop in Javascript based on php variable
body: <p>Let us say I have the following script, calculating a sum.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>    &lt;html&gt;
      &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
      &lt;script language="javascript" type="text/javascript"&gt;
  
      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;
  
      }
      &lt;/script&gt;
  
      &lt;/head&gt;        
      &lt;body&gt;
  
      &lt;?php
  
  $tR = 1;
  $maxlines = 5;
  
  echo '&lt;form name="form" method="post" action="action.php"&gt;';
  echo '&lt;table&gt;&lt;tbody&gt;';
  
  while($tR &lt;= $maxlines)
  {
  echo '&lt;tr&gt;';
  echo '&lt;td width="1"&gt;&lt;input name="price_' . $tR . '" onChange="updatesum();"         type="text"&gt;&lt;/td&gt;';
  echo '&lt;td width="1"&gt; x &lt;/td&gt;';
  echo '&lt;td width="1"&gt;&lt;input name="number_' . $tR . '" onChange="updatesum();"         type="text" value="1"&gt;&lt;/td&gt;';
  echo '&lt;/tr&gt;';
  ++$tR;
  }
  echo '&lt;/tbody&gt;&lt;/table&gt;';
  
  echo 'Total: &lt;input name="sum" value="0" type="text" readonly="readonly"&gt;';
  echo '&lt;button type="submit" name="Submit" style="display: hidden;"&gt;Submit&lt;/button&gt;';
  echo '&lt;/form&gt;';
  
      ?&gt;
  
      &lt;/body&gt;
      &lt;/html&gt;
  </code></pre>
  
  <p>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:</p>
  
  <pre><code>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;
  
  }
  </code></pre>