0

I'm new to JavaScript Programming.. I've been researching for the solution but still.. no luck. E.g: I want to add like 6 numbers (or more) that the user will input. I use this code but only the first three are calculated. When I add like four numbers already, 'NAN' appears. Nan means invalid computation.

<script type="text/javascript">
function show() {
 var a = document.calc.B1.value*1; 
 var b = document.calc.B5.value*1;
 var c = document.calc.B9.value*1;
 var d = document.calc.B12.value*1;
 var e = document.calc.B17.value*1;
 var f = document.calc.B21.value*1;

 document.calc.t1.value = a + b + c + d + e + f;
}
</script>

Only B1, B5 and B9 are calculated. Here's the working code:

<script type="text/javascript">
function show() {
 var a = document.calc.B1.value*1; 
 var b = document.calc.B5.value*1;
 var c = document.calc.B9.value*1;
 document.calc.t1.value = a + b + c;
}
</script>

Here's the form action:

<form action="sysdocadd.php" method="post" name="calc">
t1= TOTAL (text type
<td><div align="center" class="style66"><input name="t1" type="text" size="18" id="t1" value="0.00"/></div></td>

When I click calculate button, the result will be shown on t1 text area. here's the code for that..

<tr>
<td><span class="style77">Click to add</span></td>
<td><div align="center" class="style66"><input type=button onClick='show()'value=Calculate /></div></td>
</tr>

Please help me. :(


Here's the sysdocadd.php code:

<?php
$con = mysql_connect("localhost","user","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("dbconnect", $con);

$sql="INSERT INTO contents (reportnum, postedby, sysdate, userdateinp, B1, B2, B3, B4, B5, B6, B7, B8, B9, B10, B11, B12,
                            B13, B14, B15, B16, B17, B18, B19, B20, B21, B22, B23, B24, B25, B26, B27, B28, B29, B30,
                            B31, B32, B33, B34, B35, B36, B37, B38, B39, B40, B41, B42, B43, B44, B45, B46, B47, B48, B49, B50, B51, B52,
                            B53, B54, B55, B56, t1, t2, t3, t4)
                            VALUES
('$_POST[reportnum]','$_POST[postedby]','$_POST[sysdate]','$_POST[userdateinp]','$_POST[B1]','$_POST[B2]','$_POST[B3]',
'$_POST[B4]','$_POST[B5]','$_POST[B6]','$_POST[B7]','$_POST[B8]','$_POST[B9]','$_POST[B10]','$_POST[B11]','$_POST[B12]',
'$_POST[B13]','$_POST[B14]','$_POST[B15]','$_POST[B16]','$_POST[B17]','$_POST[B18]','$_POST[B19]','$_POST[B20]','$_POST[B21]',
'$_POST[B22]','$_POST[B23]','$_POST[B24]','$_POST[B25]','$_POST[B26]','$_POST[B27]','$_POST[B28]','$_POST[B29]','$_POST[B30]',
'$_POST[B31]','$_POST[B32]','$_POST[B33]','$_POST[B34]','$_POST[B35]','$_POST[B36]','$_POST[B37]','$_POST[B38]','$_POST[B39]','$_POST[B40]',
'$_POST[B41]','$_POST[B42]','$_POST[B43]','$_POST[B44]','$_POST[B45]','$_POST[B46]','$_POST[B47]','$_POST[B48]','$_POST[B49]','$_POST[B50]',
'$_POST[B51]','$_POST[B52]','$_POST[B53]','$_POST[B54]','$_POST[B55]','$_POST[B56]','$_POST[t1]','$_POST[t2]','$_POST[t3]','$_POST[t4]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con)
?> 
5
  • where is your PHP code - place where you want to add mulitiple numbers ? Commented Mar 21, 2011 at 1:48
  • This is JavaScript, not PHP.... Commented Mar 21, 2011 at 1:55
  • The code you're showing is Javascript. Is that what you meant? Commented Mar 21, 2011 at 1:57
  • @Bensiu: Here is the sysdocadd.php Commented Mar 21, 2011 at 2:03
  • @yc and cfreak: yes, sorry. Javascript. :) Commented Mar 21, 2011 at 2:07

2 Answers 2

1

You can simplify your own answer a little bit:

function addNums() {
    var sum = 0;

    for(i=0; i<14; i++)
        sum += parseFloat(document.forms["addition"]["B" + (4*i+1)].value);

    document.forms["addition"].t1.value = sum;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Problem's solved.

<script language="javascript" type="text/javascript">

function addNums(){
  num_1=Number(document.addition.B1.value);
  num_2=Number(document.addition.B5.value);
  num_3=Number(document.addition.B9.value);
  num_4=Number(document.addition.B13.value);
  num_5=Number(document.addition.B17.value);
  num_6=Number(document.addition.B21.value);
  num_7=Number(document.addition.B25.value);
  num_8=Number(document.addition.B29.value);
  num_9=Number(document.addition.B33.value);
  num_10=Number(document.addition.B37.value);
  num_11=Number(document.addition.B41.value);
  num_12=Number(document.addition.B45.value);
  num_13=Number(document.addition.B49.value);
  num_14=Number(document.addition.B53.value);
  valNum=num_1+num_2+num_3+num_4+num_5+num_6+num_7+num_8+num_9+num_10+num_11+num_12+num_13+num_14;
  document.addition.t1.value=valNum;
}
</script>

THANKS Y'ALL. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.