I want to show total sum of values entered in 'txtCal' input boxes in next input box named 'totCal' without refreshing page.
paper.php
<label>No of Questions - Easy level</label>
<input type="number" class="txtCal" id="input_item" name="level1_no_of_questions" placeholder="No of Questions - Easy level" onblur="findTotal()" Required/><br />
<label>No of Questions - Intermediate level</label>
<input type="number" name="level2_no_of_questions" class="txtCal" id="input_item" placeholder="No of Questions - Intermediate level" onblur="findTotal()" Required/><br />
<label>No of Questions - Difficult level</label>
<input type="number" name="level3_no_of_questions" class="txtCal" id="input_item" placeholder="No of Questions - Difficult level" onblur="findTotal()" Required/><br/>
<label>Total No of Questions</label>
<input type="number" name="total_no_of_questions" class="totCal" value="20" readonly="readonly" id="input_item" placeholder="Total No of Questions max="20"/><br />
<input type="submit" value="Add Exam Paper" name="submit" id="submit"/>
<form/>
Action page
question.php
<?php
if(isset($_POST['submit'])){
$level1_no_of_questions=$_POST['level1_no_of_questions'];
$level2_no_of_questions=$_POST['level2_no_of_questions'];
$level3_no_of_questions=$_POST['level3_no_of_questions'];
$total_no_of_questions=$_POST['total_no_of_questions'];
$sql2= "INSERT INTO exam_paper (level1_no_of_questions,level2_no_of_questions,level3_no_of_questions,total_no_of_questions) VALUES ('$level1_no_of_questions','$level2_no_of_questions','$level3_no_of_questions','$total_no_of_questions');
if (mysqli_query($dbcon,$sql2)){
//Redirection to the this page
header("Location:paper.php");
exit();
}else{
$error=mysqli_error($dbcon);
}
}
}
?>
<script type="text/javascript">
function findTotal(){
var arr = document.getElementsByClassName('txtCal');
var tot=0;
for(var i=0;i<arr.length;i++){
if(parseInt(arr[i].value))
tot += parseInt(arr[i].value);
}
document.getElementByClassName('totCal').value = tot;
}
</script>
This is not working..What was the error? I cant use the input id or and name.names goes to sql and id goes to design..
notworking ?onblur(). Do your fields already have data in them when you attempt to run theonblur()? You could find the error with a simpleconsole.log()in that case.