Note that there are some explanatory texts on larger screens.

plurals
  1. POJavascript / Form
    text
    copied!<p>I'm working on a project that will use loops to add many forms into a mysql database. In the javascript section i can not get the var i to work inside the function updatesum(). Can someone help me out?</p> <p>I'm tryin to aviod having to do this:</p> <pre><code>document.form1.total.value = (document.form1.number1.value -0) + (document.form1.number2.value -0); document.form2.total.value = (document.form2.number1.value -0) + (document.form2.number2.value -0); ........... document.form48.total.value = (document.form48.number1.value -0) + (document.form48.number2.value -0); document.form49.total.value = (document.form49.number1.value -0) + (document.form49.number2.value -0); </code></pre> <p>There will be about 12 other calculations that will have to take place.</p> <p>Any help would be appreciated. </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript"&gt;&lt;!-- function updatesum() { for(var i=0; i &lt; 50; i++) { document.form[i].total.value = (document.form[i].number1.value -0) + (document.form[i].number2.value -0); } } //--&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;td&gt;#&lt;/td&gt; &lt;td&gt;NUMBER&lt;/td&gt; &lt;/tr&gt; &lt;? $x=1; while($x &lt;= 50) { ?&gt; &lt;tr&gt; &lt;td&gt; &lt;? echo $x; ?&gt; &lt;/td&gt; &lt;td&gt; &lt;form name="form&lt;? echo $x;?&gt;" &gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;td&gt;NUMBER1&lt;/td&gt; &lt;td&gt;NUMBER2&lt;/td&gt; &lt;td&gt;TOTAL&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input name="number1" size="2" onChange="updatesum()" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="number2" size="2" onChange="updatesum()" /&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="total" size="3" readonly style="border:0px;"&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/td&gt; &lt;/tr&gt; &lt;? $x++; } ?&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload