If you want to do this on the client-side (with immediate response, and not submitting to database) this is a matter for JavaScript, not for PHP, which is server-side.
But you can submit data to the server, (store it in a DB ), do calculations on it and send results back to the client. The response will not be immediate, totals will only be calculated after submitting to the server.
HTH
JJ Mouris