The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Totaling Form Fields In Javascript..

I have a script to total the fields of a form, and it works great...but I am having trouble trying to figure out how to make it work when the name of the fields it is supposed to add up are php arrays.

Javascript :

Code:

<script>
function total(){
var vtemp=0
vresult=0
for (var i=1;i<27;i++){// loop to get all inputs
vtemp=eval("parseFloat(document.all.ammount"+i+".value)")
if(!isNaN(vtemp)) vresult+=vtemp// if value is a number - add it.
}
document.all.total.value=vresult// change the last input...Result one
}
</script>

Okay, so normally the field "ammount" would be "ammount1, ammount2, etc..", but on this paticular form I need it to be "ammount[id of the item], ammount[id of another item], etc..".

I am not sure how to make the javascript I have work with the php array...

Just have PHP echo into the document the ids (blue). Added onkeyup handlers; use either these or the button, depending on what you like better. Don't use the same name for the function & any form field ! (scoping issue)