I am trying to build a form calculation javascript that works fine with radio buttons, but I haven't been able to make it work with checkboxes.
The form includes a domain whois check, which generates checkboxes with the name 'domain' and the value is the checked domain's tld, its price is echoed in the same table cell. I managed to whip up a js code that matches the checked values with data from an array containing the tld prices. (see below)
Problem #1: The value of the checkbox should be the whole domain name (so that I can carry that variable with $_POST later on
Problem #2: The currently working js does not add up more domains of the same tld (foo.com and bar.com's price is added only once insted of twice) since the for loop that works well with radio buttons cannot work with multiple checks
--
Solutions I have pondered:
Solution #1: Add ID to checkboxes with their names being the searched domains and their value being the price of that domain tld and push them into a js object where the for loop could match the checked ones with their prices -- conflicts with problem #1, although I suppose I could get all checked checkboxes with jQuery and append them to a hidden input that PHP could process later on (?)..
Solution #2: Make js get the price of the domain from the table cell it's located in and add that up. -- problem: how do I make js get the price if the table cell's ID depends on what domain is searched for?
Here's the currently working js that processes only the checkboxes:
var domaintld = new Array();
domaintld["com"]=3000;
domaintld["eu"]=3650;
domaintld["org"]=3650;
domaintld["net"]=2850;
domaintld["info"]=4050;
function calcdomaintldPrice() {
var domaintldPrice=0;
var theForm = document.forms["orderform"];
var chosenDomains = theForm.elements["domain"];
for(var i=0; i < chosenDomains.length; i++) {
if(chosenDomains[i].checked) {
domaintldPrice = domaintld[chosenDomains[i].value];
break;
}
}
return domaintldPrice;
}
Here's an example of a checkbox markup:
<input type="checkbox" name="domain" value="net" id="dfbdfb.net" class="domainradio"
onclick="calculateTotal()"><label for="dfbdfb.net" class="domainradio"> </label>