0

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">&nbsp;</label>

1 Answers1

0

Ad problem 1: You might want to provide more info, such as how the html is generated. As far as I can see you could just set value to the same as id couldn't you?

Ad problem 2:

Substitute

domaintldPrice = domaintld[chosenDomains[i].value];

with

domaintldPrice += domaintld[chosenDomains[i].value];

and remove the break

kralyk
  • 4,249
  • 1
  • 32
  • 34
  • P1: I could set the value to the same as the ID but then the loop could not match it to the predefined arraylist. Because then the value would be "foo.net" and not "net". P2: Right, I don't know why I didn't think of that. Thanks. – Bert Slater Aug 17 '14 at 10:56
  • @BertSlater you can extract the TLD using [`split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) – kralyk Aug 17 '14 at 11:07
  • Tried that yesterday. Took the chosenDomains array and substringed it so I get only the part after the dot, pushed it into another array. Did not work since the checks have to be matched to the first array, not the second contaning the tld's. – Bert Slater Aug 17 '14 at 11:12
  • `var tld = chosenDomains[i].value.split('.').pop(); domaintldPrice += domaintld[tld];` – kralyk Aug 17 '14 at 11:30