Hi,
I hope i am posting at the right place. Though my question is related to Javascript, as i am using php to create an array of textfields, i think this is the right place to post.
Tables in database:
question
question_id (pk)
question
marks
marks_id(pk)
question_id
marks
I am creating an interface for the admin where he will enter marks for each question. He will enter set of questions. Later he should see list of questions and against each question a textfield where he can enter marks for each question. At the end of the form there is one textfield called total which will show the total marks. As he keep on adding marks for each question, the total field should show summed up marks.
This is the code i wrote to create the questions and marks textfield.
<?php
.....
$query=mysql_query("SELECT * FROM question");
while($fetch=mysql_fetch_array($query))
{
$question_id=$fetch['question_id'];
echo "$fetch[question] <input type='text' name=marks[$question_id] id='marks' onChange='Calculate()'> <br/>";
}
echo "<input type='text' name='total' id='total' readonly>";
?>
In javascript
<script language="JavaScript">
function Calculate()
{
var sum=0;
for (i=0;i<document.form1.marks.length;i++) {
if(document.form1.marks.value=='')
document.form1.marks.value=0;
sum += parseInt(document.form1.marks.value);
}
document.getElementById("total").value = sum;
}
</script>
The above code works fine only if the marks textfields are more than 1. If it is 1 then, the total field shows 0 even after i fill the marks textfield.
Thanks