Jump to content

Sum 3 numbers automatically

- - - - -

  • Please log in to reply
4 replies to this topic

#1
NumOne

NumOne

    Newbie

  • Members
  • Pip
  • 9 posts
Hi

In the following code, I want to sum the MideTerm, Project and the Final and write the value in the textbox of Total automatically. I wrote a javascript code but it didn't work.

<html>

<head>

[COLOR="red"]<SCRIPT> 

   function addThreeNums (inOne, inTwo, inThree) { 

      var inOne = Number(inOne); 

      var inTwo = Number(inTwo); 

      var inThree = Number(inThree); 

      return Number(inOne + inTwo + inThree); 

   } 

</SCRIPT>

[/COLOR]</head>

<body>

<title>Num One Website</title>


<?

$con = mysql_connect("mysql17.000webhost.com","a6395791_nasser","11a12a13a");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }


mysql_select_db("a6395791_StMarks", $con);


$result1 = mysql_query("SELECT * FROM Students") ;


$count=1;


echo "<form action='ConfirmEnter.php' method='post'>";

echo "<form name= 'theForm'>";

echo "<table border cellpadding=3>"; 

echo "<tr>"; 

echo "<th>ID</th>";

echo "<th>MidTerm</th>"; 

echo "<th>Project</th>"; 

echo "<th>Final</th>";

echo "<th>Total</th>". "</tr>";

while($row1 = mysql_fetch_array($result1)) 

 { 

 echo "<tr>";

 echo "<td><input name='ID' readonly='readonly' value='". $row1['ID'] ."' size=5/></td> "; 

 echo "<td><input type='text' name='mt' size=5 value='0.0'/></td>";

 echo "<td><input type='text' name='pr' size=5 value='0.0' /></td>";

 echo "<td><input type='text' name='fi' size=5 value='0.0' /></td>";

 [COLOR="red"]echo "<td><input type='text' name='tot' size=5 value='document.write(addThreeNums(theForm.mt.value,theForm.pr.value,theForm.fi.value));' /></td>";

[/COLOR] echo "</tr>";

 } 

echo "</table>"; 

echo "<input type='submit' value='Submit' />";

echo "</form>"; 

echo "</form>"; 

mysql_close($con);



?>


</body>

</html>


Thank you

#2
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
I took a look at it and made this what works fine for me. PS is that your real log in for mysql????

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Page title</title>

<script language="JavaScript" type="text/javascript">
<!--
function addThreeNums (inOne, inTwo, inThree) {

var Addall = inOne + inTwo + inThree;
alert(Addall);
}

//-->
</script>

<a href="javascript:addThreeNums(5,2,3)">test</a>

</head>
<body>



</body>
</html>
"The question of whether a computer can think is no more interesting than the question of whether a submarine can swim." (Edsger Dijkstra)

#3
NumOne

NumOne

    Newbie

  • Members
  • Pip
  • 9 posts
what I need is sum the midterm mark + project mark + final mark and let the result of the sum automatically in the textbox.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
By automatically I assume you mean on each change, you would need to use the onKeyUp or onChange attributes in each textbox.

You can pass the textboxname.innerHTML or whatnot to get the values.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Upstream

Upstream

    Learning Programmer

  • Members
  • PipPipPip
  • 44 posts
In your script I see a lot of things that will not work I think. Maybe you should look at the basics of js to get it to work? Or try to do it all with PHP?
"The question of whether a computer can think is no more interesting than the question of whether a submarine can swim." (Edsger Dijkstra)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users