# help with javascript Calculations radio box

• 09-24-2013, 11:56 PM
xenaworx
help with javascript Calculations radio box
Can anyone help me
I trying to do a piece of coding which has two groups of radio button in them one that selects the water system and the time it take and the other one selects which soil type and how many time to water the soil for. I al so have a checked box if there are large plants to be watered which added and extra 1.5 time on
I need to validate also if the radio button have been checked or not.
I am have problems working on the calculation being a newbie at all this JavaScript thing

<!doctype html>
<html>
<meta charset="utf-8">
<title>Home Gardening Watering guide</title>
<script>

//Check the users responses and calculate the fare
function fareCalc() {
var checkbox; // A checkbox
var watersystem = 0; //The number of the fare selected
var soilType = 0; //The number of the seat
var wateringTime = 0; //Total cost of the fare

//Find the valueof the waater system button
for (var i = 1; i <=4; i++) {
radioButton1 = document.getElementById("water" + i);
if (radioButton1.checked == false ) {
}
else
}

//Check if there are large plant involed
for (var i = 1; i <=1; i++) {
checkbox = document.getElementById("trees");
if (checkbox.checked == true) {
trees = "1.5";
}
if (checkbox.checked == false) {
trees = "0";
}
}
// Get the selected soil type
for (var i = 1; i <=3; i++) {
radioButton2 = document.getElementById("soil" + i);
if (radioButton2.checked == true) {
}
}
// alert if no seating class selected
if (soilType == 0) {
}

//Display total cost of selected fare
wateringTime = watersystem * trees * soilType ;
//Round to the nearest number
wateringTime = Math.round(wateringTime);
//Turn string into Integer
wateringTime = parseInt(wateringTime);

//Display total cost of flight
alert ("The cost of the flight is \$ " + wateringTime);
}
</script>

<body>
<h1>Gotham Airlines Fare Calculator</h1>
<p>Complete the form below to calculate the cost of your flight.</p>
<form>
<p>Route:<br>
<input type="radio" id="water1" name="water" value="6"> <label for="water1">Jet Spray </label><br>
<input type="radio" id="water2" name="water" value="10"><label for="water2">Sprinkler </label><br>
<input type="radio" id="water3" name="water" value="17"><label for="water3">Mini Sprinkler</label><br>
<input type="radio" id="water4" name="water" value="50"><label for="water4">Dripper </label><br>

</p>
<p>Click here if you will be purchasing a return fare:<input type="checkbox" id="treeses" name="trees"><label for="trees" > </label><br></p>
<p>Seating:<br>
<input type="radio" id="soil1" name="soil" value="2"><label for="seat1">Sand</label><br>
<input type="radio" id="soil2" name="soil" value="1.5"><label for="seat2">Loam</label><br>
<input type="radio" id="soil3" name="soil" value="1"><label for="seat3">Clay</label></p>
<p><input type="submit" value="Calculate" onClick="fareCalc();"> <input type="reset" ></p>

</form>
</body>
</html>
• 09-25-2013, 07:01 AM
vwphillips
Code:

```<!doctype html> <html> <head> <meta charset="utf-8"> <title>Home Gardening Watering guide</title> <script> //Check the users responses and calculate the fare function fareCalc(frm) {  var rads=frm['water'],w,s,t,z0=0,mess=[];  for (;z0<rads.length;z0++){   if (rads[z0].checked){   w=rads[z0].value;   break;   }  }  if (!w){   mess.push('Click Water');  }  rads=frm['soil'],z0=0;  for (;z0<rads.length;z0++){   if (rads[z0].checked){   s=rads[z0].value;   break;   }  }  if (!s){   mess.push('Click Soil');  }  if (s&&w){   t=s*w;   rad=frm['large']   if (rad.checked){   t*=rad.value;   }   alert(t.toFixed(2));   return false; // change to true to submit the form  }  alert(mess.join('\n'));  return false; } </script> </head> <body> <form onsubmit="return fareCalc(this);"> <p>Water Type:<br> <input type="radio" name="water" value="6"> <label for="water1">Jet Spray </label><br> <input type="radio" name="water" value="10"><label for="water2">Sprinkler </label><br> <input type="radio" name="water" value="17"><label for="water3">Mini Sprinkler</label><br> <input type="radio" name="water" value="50"><label for="water4">Dripper </label><br> </p> <p>Soil Type:<br> <input type="radio" name="soil" value="2"><label for="seat1">Sand</label><br> <input type="radio" name="soil" value="1.5"><label for="seat2">Loam</label><br> <input type="radio" name="soil" value="1"><label for="seat3">Clay</label></p> <input type="checkbox" name="large" value="1.5" /> Large Plants <p><input type="submit" value="Calculate"  <input type="reset" ></p> </form> </body> </html>```

## X vBulletin 4.2.2 Debug Information

• Page Generation 0.07000 seconds
• Memory Usage 2,370KB
• Queries Executed 11 (?)
Template Usage (20):
• (1)bbcode_code_printable
• (1)footer
• (1)gobutton
• (1)navbar_moderation
• (1)navbar_noticebit
• (2)option
• (1)spacer_close
• (1)spacer_open

Phrase Groups Available (3):
• global
• postbit
Included Files (19):
• ./global.php
• ./includes/class_bootstrap.php
• ./includes/init.php
• ./includes/class_core.php
• ./includes/config.php
• ./includes/functions.php
• ./includes/class_friendly_url.php
• ./includes/class_hook.php
• ./includes/class_bootstrap_framework.php
• ./vb/vb.php
• ./vb/phrase.php
• ./includes/functions_calendar.php
• ./includes/class_bbcode_alt.php
• ./includes/class_bbcode.php
• ./includes/functions_bigthree.php
• ./includes/functions_notice.php

Hooks Called (41):
• init_startup
• init_startup_session_setup_start
• database_pre_fetch_array
• database_post_fetch_array
• init_startup_session_setup_complete
• global_bootstrap_init_start
• global_bootstrap_init_complete
• cache_permissions
• fetch_foruminfo
• global_state_check
• global_bootstrap_complete
• global_start
• style_fetch
• global_setup_complete
• bbcode_fetch_tags
• bbcode_create
• bbcode_parse_start
• bbcode_parse_complete_precache
• bbcode_parse_complete
• cache_templates
• cache_templates_process
• template_register_var
• template_render_output
• fetch_template_start
• fetch_template_complete
• parse_templates