Calcolare la somma

gasparroa

Utente Attivo
4 Dic 2008
57
0
0
Ciao ho una funzione che calcola la somma in automatico , vorrei farla calcolare in automatica su + campi.

mi spiego

il primo campo testo prende in numero in entrata mentre il secondo lo prende dal db (io per postarlo ho impostato un valore di default) il terzo calcola il risultato.

il primo valore e fisso

es.

primo=20

secondo=5
somma=25

secondo1=4
somma1=24

secondo2=7
somma2=27

ecc.

Ho preso uno script dal web , ho inserito dei campi facendo un ciclo con php ma non funziona più.

di javascript ho scarsissima conoscenza e non so da quale punto inziare a modificare la funzione per far eseguire l'operazione.
se nel ciclo al posto <=5 metto <1 funziona come posso risolvere



PHP:
<script>  
function somma() { 

a=eval(document.all.prima.value); 

b=eval(document.all.seconda.value); 

c=a+b; 

document.all.terza.value=c; 

} 

</script> 

Questo è un programma che fa la somma di due numeri.<br> 

Scrivi qui il primo numero: 

<input type="text" name="prima" onKeyUp="somma()"> <br> 

<?php  
for($i=0;$i<=5;$i++) 
{ 
?> 
Scrivi qui il secondo numero<input name="seconda" type="text" onKeyUp="somma()" value="5">  

risultao:  
<input type="text" name="terza"><br> 
<?php  
} 
?>
 
ho apportato delle modifiche allo script solo che la funzione è troppo lunga ed è limitata a 5 elementi si puo semplificare e farla funzionare per n elementi

mi dareste un aiuto che non riesco

grazie

PHP:
<script>
function somma() { 
var a=eval(document.getElementById("primo").value) ; 
var b=eval(document.getElementById("secondo0").value);
var somma0=(a) - ((a) * (b)) / 100;
var somma0=Math.round(somma0 * 100) / 100;
document.getElementById("somma0").value=somma0; 

var b1=eval(document.getElementById("secondo1").value);
var somma1=(a) - ((a) * (b1)) / 100;
var somma1=Math.round(somma1 * 100) / 100;
document.getElementById("somma1").value=somma1; 

var b2=eval(document.getElementById("secondo2").value);
var somma2=(a) - ((a) * (b2)) / 100;
var somma2=Math.round(somma2 * 100) / 100;
document.getElementById("somma2").value=somma2; 

var b3=eval(document.getElementById("secondo3").value);
var somma3=(a) - ((a) * (b3)) / 100;
var somma3=Math.round(somma3 * 100) / 100;
document.getElementById("somma3").value=somma3; 

var b4=eval(document.getElementById("secondo4").value);
var somma4=(a) - ((a) * (b4)) / 100;
var somma4=Math.round(somma4 * 100) / 100;
document.getElementById("somma4").value=somma4; 
}
</script>
<input type="text" id="primo" onKeyUp="somma()" /><br /><br />
<?php 
for($i=0;$i<=4;$i++)
 {
?>

<input type="text" id="secondo<?php echo $i; ?>" name="secondo" onKeyUp="somma()" value="6" />
<input type="text" id="somma<?php echo $i; ?>" /><br />
<?php 
 }
?>
 
Ho eliminato eval dalla funzione per fare delle prove ma non riesco a semplificare la funzione

PHP:
<script>
function somma() { 
var a=document.getElementById("primo").value; 

var b0=document.getElementById("secondo0").value;
var somma0=(a) - ((a) * (b0)) / 100;
var somma0=Math.round(somma0 * 100) / 100;
document.getElementById("somma0").value=somma0; 

var b1=document.getElementById("secondo1").value;
var somma1=(a) - ((a) * (b1)) / 100;
var somma1=Math.round(somma1 * 100) / 100;
document.getElementById("somma1").value=somma1; 

var b2=document.getElementById("secondo2").value;
var somma2=(a) - ((a) * (b2)) / 100;
var somma2=Math.round(somma2 * 100) / 100;
document.getElementById("somma2").value=somma2; 

var b3=document.getElementById("secondo3").value;
var somma3=(a) - ((a) * (b3)) / 100;
var somma3=Math.round(somma3 * 100) / 100;
document.getElementById("somma3").value=somma3; 
}
<input type="text" id="primo" onKeyUp="somma()" /><br /><br />
<?php 
for($i=0;$i<=3;$i++)
 {
?>

<input type="text" id="secondo<?php echo $i; ?>" name="secondo" onKeyUp="somma()" value="6" />
<input type="text" id="somma<?php echo $i; ?>" /><br />
<?php 
 }
?>
 

Discussioni simili