/    Sign up×
Community /Pin to ProfileBookmark

Javascript calculation is not working

Can someone PLEASE show me what I am doing wrong with my code here?

Basically, I have a registration form. In this registration form, I have 4 people register. It depends on each person birthyear to determine the amount of fees for each person.

The fees are: Registration Fee, Hotel Fee, Meal Fee, Discount

[B]Also, the trick is only first and second persons get Hotel Fee and Discount Fee. Third and Fourth person only pay Registration Fee and Meal Fee.[/B]

So when user select birthyear from a dropdown menu for each person, the fees will be automatically filled in the input fields for each person and then do a total calculation.

Each person will have a total of fees and there is another total field to automatically total up all 4 total fields from those 4 people.

So here is my code so far

Javascript

[CODE]
<script type=”text/javascript”>
function doUpdate(year, num)
{
if (isNaN(year)) return;
var regfee = ‘0’;
var meal = ‘0’;
var hotel = ‘0’;
//REGISTRATION Price
if (year >= 2000)
{
regfee = ‘0’;
}
else if (year < 2000 && year > 1995 )
{
regfee = ’20’;
}
else
{
regfee = ’30’;
}
//MEAL Price
if (year < 2001)
{
meal = ’80’;
}
else if (year >= 2001 && year <= 2009)
{
meal = ’60’;
}
else
{
meal = ‘0’;
}
// HOTEL Price
if(document.getElementById(‘year1′).value!=”)
{
hotel=’199’;
}
else if (document.getElementById(‘year2′).value!=”)
{
hotel=’199’;
}

//DISCOUNT Price
if (document.getElementById(‘vip’+num).checked==true)
{
if (year > 1993)
{
discount = ‘0’;
}
else if (year == 1993)
{
discount = ‘0’;
}
else
{
discount = ‘-20’;
}
}
else
{
if (year > 1993)
{
discount = ‘0’;
}
else if (year == 1993)
{
discount = ‘0’;
}
else
{
discount = ‘-10’;
}

}

document.getElementById(‘regfee’ + num).value = regfee;
document.getElementById(‘meal’ + num).value = meal;

if(document.getElementById(‘year1’).value!=”){

document.getElementById(‘hotel1’).value = hotel;
document.getElementById(‘discount1’).value = discount;

//Write to total field for second person
document.getElementById(‘total1’).value = parseInt(regfee)+parseInt(meal)+parseInt(hotel)+parseInt(discount);
}
if(document.getElementById(‘year2’).value!=”){
document.getElementById(‘hotel2’).value = hotel;
document.getElementById(‘discount2’).value = discount;

//Write to total field for second person
document.getElementById(‘total2’).value = parseInt(regfee)+parseInt(meal)+parseInt(hotel)+parseInt(discount);
}
if(document.getElementById(‘year3’).value!=” && document.getElementById(‘year4’).value!=”){
document.getElementById(‘hotel3’).value == ‘0’;
document.getElementById(‘discount3’).value == ‘0’;
document.getElementById(‘hotel4’).value == ‘0’;
document.getElementById(‘discount4’).value == ‘0’;

// Write to total fields for third and fourth person
document.getElementById(‘total3’).value = parseInt(regfee)+parseInt(meal);
document.getElementById(‘total4’).value = parseInt(regfee)+parseInt(meal);
}

// Write to subtotal field by sum up all 4 total fields
document.getElementById(‘subtotal1’).value = parseInt(document.getElementById(‘total1’).value) + parseInt(document.getElementById(‘total2’).value) + parseInt(document.getElementById(‘total3’).value) + parseInt(document.getElementById(‘total4’).value);
}

</script>
[/CODE]

and here is my form

[CODE]
<form action=”form.html” method=”post” class=”js_submit” name=”form1″ id=”form1″>
<table>
<tr>
<td>VIP</td>
<td>Birthyear</td>
<td>Registration Fee</td>
<td>Hotel Fee</td>
<td>Meal Fee</td>
<td>Discount</td>
<td>Total</td>
</tr>
<!———–First Persion ——————->
<tr>
<td><input type=”checkbox” name=”vip1″ id=”vip1″ value=”Y” class=”inpt_c” /></td>
<td><select name=”year1″ id=”year1″ onchange=’doUpdate(this.value * 1,1);’>
<option selected=”selected”></option>
<option value=”2011″>2011<option>
<option value=”2010″>2010</option>
<option value=”2009″>2009</option>
<option value=”2008″>2008</option>
<option value=”2007″>2007</option>
<option value=”1989″>1989</option>
<option value=”1988″>1988</option>
<option value=”1987″>1987</option>
<option value=”1986″>1986</option>
<option value=”1985″>1985</option>
<option value=”1984″>1984</option>
<option value=”1983″>1983</option>
<option value=”1982″>1982</option>
<option value=”1981″>1981</option>
</select></td>
<td><input type=”text” id=”regfee1″ name=”regfee1″ class=”inpt_a” value=””/></td>
<td><input type=”text” value=”” id=”hotel1″ name=”hotel1″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”meal1″ name=”meal1″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”discount1″ name=”discount1″ class=”inpt_a”/></td>
<td><input type=”text” value=”” id=”total1″ name=”total1″ class=”inpt_a” /></td>
</tr>
<!————Second Person —————–>
<tr>
<td><input type=”checkbox” name=”vip2″ id=”vip2″ value=”Y” class=”inpt_c” /></td>
<td><select name=”year2″ id=”year2″ onchange=’doUpdate(this.value * 1,2);’>
<option selected=”selected”></option>
<option value=”2011″>2011<option>
<option value=”2010″>2010</option>
<option value=”2009″>2009</option>
<option value=”2008″>2008</option>
<option value=”2007″>2007</option>
<option value=”2006″>2006</option>
<option value=”1989″>1989</option>
<option value=”1988″>1988</option>
<option value=”1987″>1987</option>
<option value=”1986″>1986</option>
<option value=”1985″>1985</option>
<option value=”1984″>1984</option>
<option value=”1983″>1983</option>
<option value=”1982″>1982</option>
<option value=”1981″>1981</option>
</select></td>
<td><input type=”text” id=”regfee2″ name=”regfee2″ class=”inpt_a” value=””/></td>
<td><input type=”text” value=”” id=”hotel2″ name=”hotel2″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”meal2″ name=”meal2″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”discount2″ name=”discount2″ class=”inpt_a”/></td>
<td><input type=”text” value=”” id=”total2″ name=”total2″ class=”inpt_a” /></td>
</tr>
<!———–Third Person ——————->
<tr>
<td><input type=”checkbox” name=”vip3″ id=”vip3″ value=”Y” class=”inpt_c” /></td>
<td><select name=”year3″ id=”year3″ onchange=’doUpdate(this.value * 1,3);’>
<option selected=”selected”></option>
<option value=”2011″>2011<option>
<option value=”2010″>2010</option>
<option value=”2009″>2009</option>
<option value=”2008″>2008</option>
<option value=”2007″>2007</option>
<option value=”2006″>2006</option>
<option value=”2005″>2005</option>
<option value=”1990″>1990</option>
<option value=”1989″>1989</option>
<option value=”1988″>1988</option>
<option value=”1987″>1987</option>
<option value=”1986″>1986</option>
<option value=”1985″>1985</option>
<option value=”1984″>1984</option>
<option value=”1983″>1983</option>
<option value=”1982″>1982</option>
<option value=”1981″>1981</option>
</select></td>
<td><input type=”text” id=”regfee3″ name=”regfee3″ class=”inpt_a” value=””/></td>
<td><input type=”text” value=”” id=”hotel3″ name=”hotel3″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”meal3″ name=”meal3″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”discount3″ name=”discount3″ class=”inpt_a”/></td>
<td><input type=”text” value=”” id=”total3″ name=”total3″ class=”inpt_a” /></td>
</tr>
<!———-Fourth Person ——————–>
<tr>
<td><input type=”checkbox” name=”vip4″ id=”vip4″ value=”Y” class=”inpt_c” /></td>
<td><select name=”year1″ id=”year1″ onchange=’doUpdate(this.value * 1,4);’>
<option selected=”selected”></option>
<option value=”2011″>2011<option>
<option value=”2010″>2010</option>
<option value=”2009″>2009</option>
<option value=”2008″>2008</option>
<option value=”2007″>2007</option>
<option value=”2006″>2006</option>
<option value=”2005″>2005</option>
<option value=”1989″>1989</option>
<option value=”1988″>1988</option>
<option value=”1987″>1987</option>
<option value=”1986″>1986</option>
<option value=”1985″>1985</option>
<option value=”1984″>1984</option>
<option value=”1983″>1983</option>
<option value=”1982″>1982</option>
<option value=”1981″>1981</option>
</select></td>
<td><input type=”text” id=”regfee4″ name=”regfee4″ class=”inpt_a” value=””/></td>
<td><input type=”text” value=”” id=”hotel4″ name=”hotel4″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”meal4″ name=”meal4″ class=”inpt_a” /></td>
<td><input type=”text” value=”” id=”discount4″ name=”discount4″ class=”inpt_a”/></td>
<td><input type=”text” value=”” id=”total4″ name=”total4″ class=”inpt_a” /></td>
</tr>
<!————Subtotal of all 4 totals above ————>
<tr>
<td colspan=”6″> <input name=”subtotal1″ type=”text” id=”subtotal1″ class=”inpt_a” value=”” readonly=”readonly” /></td>
</tr>
</form>
[/CODE]

Here is my form URL: [URL=http://daihoibaptit.org/form.html]http://daihoibaptit.org/form.html[/URL]

Thanks for your help

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@jalarieJan 13.2012 — Input from a form field is a string. Start by multiplying "year" by 1 to force it to be a number. Maybe that will help.
<i>
</i>year=year*1;
×

Success!

Help @chichai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...