lundi 13 janvier 2020

How to implement an if-statement in an order form


I'm building a simple order form with some calculations with number & hidden fields (for email and autoresponder).
First, when an input field is not '0' i want it to calculate the delivery cost in the total price.
Second, if the values for product A and B are not '0', i want only the delivery cost of A in the total, and not both A + B.

I'v tried several possibilities but I'm kinda new in coding so I don't know the right terms for everything and therefor search better. Maybe I'm totally wrong or maybe it's just a simple syntax error. Hope someone can help!

This is the function that pulls the values from the user defined fields:

function getAantal(){
var AantalBoek = document.getElementById('AantalBoek').value;
var AantalCD = document.getElementById('AantalCD').value;

The delivery cost fields are hidden and have a fixed value, VerzendingBoek = 7, VerzendingCD = 2. With the 'if' factor i want the delivery cost calculated in the total when the values (of the variables above) are not null or empty. i.e. when value = 0, delivery is 0. When value = 1,2 or 3 ... then delivery = 7

var VerzendingBoek = document.getElementById('VerzendingBoek').value; 
if(AantalBoek !== null && AantalBoek !== '') { VerzendingBoek = 0;}

This includes the second part of my question: when the user orders both items, only the delivery cost of VerzendingBoek should be used = 7 and not 7 + 2.

var VerzendingCD = document.getElementById('VerzendingCD').value;
if(AantalCD !== null && AantalCD !== '') { VerzendingCD = 0; }
if(VerzendingBoek !== 7) { VerzendingCD = 0; }

Here I calculate the totals:

var TotaalVerzending = +VerzendingBoek + +VerzendingCD;
document.getElementById('Verzending').value=TotaalVerzending;
}

Aucun commentaire:

Enregistrer un commentaire