I will try your method. I have found a way to solved this problem of mine. I have four fields I may or may not choose to fill up one out of the four fields, or I might filled up all of them. So when I leave one or two fields empty, and add all the value up, I will get NaN. For those empty fields, it would be better to check and see whether it is blank, e.g. if (field1 == '') { field=0; } And then add them all up from there. Riccardi Moreno wrote: > Try this: > > The HTML codes should look like this: > <input type="text" name="field[]" onChange="add();"> > <input type="text" name="field[]" onChange="add();"> > <input type="text" name="field[]" onChange="add();"> > <input type="text" name="field[]" onChange="add();"> > <input type="text" name="totalsum"> > > function add() { > var total = 0; > var thisform = document.forms[0]; > for (i=0;i<=thisform.elements["field[]"].length;i++) { > if (!isNaN(thisform.elements["field[]"].value) total += > thisform.elements["field[]"].value; > } > thisform.totalsum.value=total; > } > > -----Messaggio originale----- > Da: Shahmat Dahlan [mailto:shahmatd@sains.com.my] > Inviato: venerdi 11 ottobre 2002 11.35 > A: php-db@lists.php.net > Oggetto: Javascript > > My question might have anything to do with PHP but I have the following > scenario: > > Say I have four fields > field1 : XXXXXXXXXXXX > field2 : XXXXXXXXXXXX > field3 : XXXXXXXXXXXX > field4 : XXXXXXXXXXXX > > And a total sum field, which is a total sum of the value of the > expression, totalsum = field1 + field2 + field3 + field4 > Total sum : XXXXXXXXXXX > > You would need to use Javascript to extract the value of field1, field2, > field3, field4, initiated by the Javascript onChange event. > > The HTML codes should look like this: > <input type="text" name="field1" addChange="add();"> > <input type="text" name="field2" addChange="add();"> > <input type="text" name="field3" addChange="add();"> > <input type="text" name="field4" addChange="add();"> > <input type="text" name="totalsum"> > > So the add function snippet that I have: > > function add() > { > var thisform=document.forms[0]; > var field1=thisform.field1.value; > var field2=thisform.field2.value; > var field3=thisform.field3.value; > var field4=thisform.field4.value; > var totalqty=field1+field2+field3+field4; > thisform.totalsum.value=totalqty; > } > > But all I got is a NaN which means Not A Number. I understand that you > solve this by using the parseInt Javascript function. > But still all i get is a NaN value. > > Would appreciate if someone could help me out on this. > > Regards > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php
Attachment:
smime.p7s
Description: S/MIME Cryptographic Signature