|
|||||||
| JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...) |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
problems with calculation
I'm creating a form which user can use to order some dvd's. It should be rewritten once in PHP, but that's not important. All I want now is the form to function so I can use a mailscript from my ISP to send the neccesary data.
The problem is the script won't work the right way yet: calculation of the total price (t) isn't working the way I want it. When I check one of the checkboxes, the value is displayed in the "totaal" field, but when I check another, the value is replaced and not added as needed. Can anyone tell me where my mistake lies? (Might be something with strings/values...) I placed the entire script so anyone can test it and see the problem. (Tested with Firefox) René Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="../scripts/style.css">
<script language="Javascript">
<!--
function prijs()
{
Keukenhof = false;
Volendam = false;
Windmolen = false;
Grand_tour = false;
Citytour = false;
None = true;
a = "";
b = "";
c = "";
d = "";
e = "";
if (window.document.bestel.Keukenhof.checked == true) {
Keukenhof = true;
}
else {
if (window.document.bestel.Volendam.checked == true) {
Volendam = true;
}
else {
if (window.document.bestel.Windmolentour.checked == true) {
Windmolen = true;
}
else {
if (window.document.bestel.Grand_Tour_Holland.checked == true) {
Grand_tour = true;
}
else {
if (window.document.bestel.Citytour.checked == true) {
Citytour = true;
}
}
}
}
}
if (Keukenhof == true) {
a = window.document.bestel.Keukenhof.value;
}
else {
if (Volendam == true) {
b = window.document.bestel.Volendam.value;
}
else {
if (Windmolen == true) {
c = window.document.bestel.Windmolentour.value;
}
else {
if (Grand_tour == true) {
d = window.document.bestel.Grand_Tour_Holland.value;
}
else {
if (Citytour == true) {
e = window.document.bestel.Citytour.value;
}
else {
t = 0,00;
}
}
}
}
}
t = a + b + c + d + e;
window.document.bestel.totaal.value = t;
}
//-->
</script>
</head>
<body>
<center><p class="header">Bestelformulier</center>
<br><br>
<p class="text">Door middel van onderstaand formulier kunt u de door u gewenste producten bestellen.<br>
Het formulier bevat drie onderdelen: naw gegevens, de bestelling en de betalingsgegevens.<br>
Uw gegevens worden niet aan derde gegeven of doorverkocht.
<br><br>
<form name="bestel">
<p class="form">
Deel 1: Naam, Adres en Woonplaatsgegevens.<br><br>
Voornaam/Voorletter(s): <input type="text" name="voornaam" value=""><br>
Achternaam: <input type="text" name="achternaam" value=""><br>
Straatnaam: <input type="text" name="Straat" value=""> Huisnummer: <input type="text" name="nummer" value=""><br>
Postcode: <input type="text" name="Postcode" value=""> Woonplaats: <input type="text" name="Woonplaats" value="">
<br><br>
Deel 2: De bestelling<br>
Vink aan welke producten u graag ontvangt.<br><br>
<input type="checkbox" name="Keukenhof" value=19,95 onChange="prijs()"> De Keukenhof<br>
<input type="checkbox" name="Volendam" value=14,95 onChange="prijs()"> Volendam<br>
<input type="checkbox" name="Windmolentour" value=19,95 onChange="prijs()"> Windmolentour<br>
<input type="checkbox" name="Grand_Tour_Holland" value=19,95 onChange="prijs()"> Grand Tour Holland<br>
<input type="checkbox" name="Citytour" value=14,95 onChange="prijs()"> Citytour<br>
<br>
<br>
<input type="textbox" name = "totaal" value="0,00">
</form>
</body>
</html>
__________________
Don't take life to serious, you won't survive it anyway... |
|
#2
|
||||
|
||||
|
you if / else / if / else .................. if / elss x 20 is borked, since it will only ever do one of a, b, c, d......
Code:
if (Keukenhof == true) {
a = window.document.bestel.Keukenhof.value;
}
if (Volendam == true) {
b = window.document.bestel.Volendam.value;
}
if (Windmolen == true) {
c = window.document.bestel.Windmolentour.value;
}
if (Grand_tour == true) {
d = window.document.bestel.Grand_Tour_Holland.value;
}
if (Citytour == true) {
e = window.document.bestel.Citytour.value;
}
Code:
if (Keukenhof == true) {
a = window.document.bestel.Keukenhof.value;
}
else {
if (Volendam == true) {
b = window.document.bestel.Volendam.value;
}
else {
if (Windmolen == true) {
c = window.document.bestel.Windmolentour.value;
}
else {
if (Grand_tour == true) {
d = window.document.bestel.Grand_Tour_Holland.value;
}
else {
if (Citytour == true) {
e = window.document.bestel.Citytour.value;
}
else {
t = 0,00;
}
}
}
}
}
__________________
Richard Turner - A Virtual Insight |
|
#3
|
|||
|
|||
|
Right... stupid me
Thanks for the hint. René
__________________
Don't take life to serious, you won't survive it anyway... |
|
#4
|
|||
|
|||
|
Rene:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">
function prijs(nBox){
var currTotaal = Number(document.forms[0]['totaal'].value.replace(",","."));
if (nBox.checked)
{
document.forms[0]['totaal'].value = ((currTotaal + Number(nBox.value)).toFixed(2)).replace(".",",");
}
else {
document.forms[0]['totaal'].value = ((currTotaal - Number(nBox.value)).toFixed(2)).replace(".",",");
}
}
</script>
<style type="text/css">
form {width:420px;margin:auto}
fieldset {padding:5px;background-color:#f0fff0;}
legend {font-size:14pt;color:#00008b;background-color:#afeeee;padding:3px;margin:10px;font-family:arial}
label {font-size:12pt;margin:5px}
</style>
</head>
<body>
<form>
<fieldset>
<legend> Bestelformulier </legend>
<label> Deel 1: Naam, Adres en Woonplaatsgegevens.</label>
<br><br>
<label> Voornaam/Voorletter(s): <input type="text" name="voornaam" size="15"></label>
<br>
<label> Achternaam: <input type="text" name="achternaam" size="20"></label>
<br>
<label> Straatnaam: <input type="text" name="Straat" size="20"></label> <label>Huisnummer: <input type="text" name="nummer" size="6"></label>
<label> Postcode: <input type="text" name="Postcode" size="6"></label> <label> Woonplaats: <input type="text" name="Woonplaats" size="20"></label>
<br><br>
<label>Deel 2: De bestelling </label>
<br>
<label> Vink aan welke producten u graag ontvangt.</label>
<br><br>
<label><input type="checkbox" name="Keukenhof" value="19.95" onclick="prijs(this)"> De Keukenhof </label>
<br>
<label><input type="checkbox" name="Volendam" value="14.95" onclick="prijs(this)"> Volendam </label>
<br>
<label><input type="checkbox" name="Windmolentour" value="19.95" onclick="prijs(this)"> Windmolentour </label>
<br>
<label><input type="checkbox" name="Grand_Tour_Holland" value="19.95" onclick="prijs(this)"> Grand Tour Holland </label>
<br>
<label><input type="checkbox" name="Citytour" value="14.95" onclick="prijs(this)"> Citytour </label>
<br><br>
<label><input type="textbox" name="totaal" value="0,00" size="6" readonly> Totaal </label>
<div style='text-align:center;padding-top:5px'>
<button type='submit'> Submit </button>
</div>
</fieldset>
</form>
</body>
</html>
Last edited by James Gatka; 07-19-2006 at 04:51 PM. |
|
#5
|
|||
|
|||
|
More advanced way... thank you
René
__________________
Don't take life to serious, you won't survive it anyway... |
|
#6
|
|||
|
|||
|
Rene:
You're welcome. Take care. I realized, too late, the information was Name, Address, so forth. I edited my previous post to "widen" the form, and boxes. |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|