1
Situation: You have the form and one of the options I want is that it gives a result of the sum of variables automatically by clicking a button or it can be without clicking, only the user just typing the values.
The problem is: he is already activating the action for the processing page in php without the user finishing the form. How could you show the result of the sum below the add-up button (the same one that is activating the action)?
code in header:
<script type="text/javascript">
function calcula(){
var val1 = document.getElementById("pa").value;
var val2 = document.getElementById("di").value;
var val3 = document.getElementById("insc").value;
var val4 = document.getElementById("outr").value;
var soma = parseFloat(val1)+parseFloat(val2)+parseFloat(val3)+parseFloat(val4);
document.write("" + soma);
}
</script>
now the code within the html form calling the above function:
<legend>Calcular total.</legend>
<button id="calcular" onclick="calcula()">Calcular</button>
I accept suggestions to do otherwise or other language.
Nothing in this code is triggering the action, but the
document.write
is overwriting the entire document.– bfavaretto
The user puts the numbers in the variables, would like a button or even the sum of these fields were made and shown. the code ai is part inside the form in HTML, I could use
alert(soma);
it shows the result (as I want) only that after the form gives the action for me to handle the information on another PHP page, but I do not want it. I just want to show the result of these variables automatically summed within the form itself that the user is filling.– Vinicius
One thing q tb can be done is to place the call from the calculus function on the onchange of values input, so vc has the sum value before pressing the button.
– CCastro