There are many things in the code you provided, and have nothing to do with PHP but with JavaScript and DOM. First of all you have to validate that it is an integer what inserted. Then you should update and if a field has no value than it should considered as zero. When you add to a total variable this must has JavaScript global scope in order to abstract it. You can see it in code bellow, just ask specific if anything isn’t clear enough.