It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

DoCalucate() function

DoCalucate() function

Postby jacinla on Tue Apr 01, 2008 12:04 am

Hi everyone.

I found the script below and it works great... I need to keep a running total on a registration php page. What I would like to change is using a radio box or check box to get the total. Can this be modified to do this? Right now on my form, since the checkbox / radiobox has a value attached to it it does not calculate correctly (it just takes the first box checked and adds it in).

I really appreciate your input! Thank you!

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test</title>
<script language="javascript">

var temp1 = 0;
var temp2 = 0;

function doCalculate() {

member = document.getElementById('noTicMem').value;
nonmember = document.getElementById('noTicNon').value;


if(member != '') {
member = member * 2;
temp1 = member;
} else { temp1 = 0; }

if(nonmember != '') {
nonmember = nonmember * 5;
temp2 = nonmember;
} else { temp2 = 0; }

var total = temp1 + temp2;
document.getElementById('total').value = total;
}

</script>
</head>
jacinla
 
Posts: 4
Joined: Mon Mar 31, 2008 11:57 pm

Postby rangana on Tue Apr 01, 2008 4:36 am

Could you show us the other half of the page?..the one that contains the NoTicMem and NoTicNon id's?..

As far as I can tell, you haven't declared the member and nonmember varibale yet:

var member = document.getElementById('noTicMem').value;
var nonmember = document.getElementById('noTicNon').value;
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

function doCalculate()

Postby jacinla on Tue Apr 01, 2008 2:18 pm

This is the top portion of the javascript as I modified it.

<script language="javascript">

var temp1 = 0;
var temp2 = 0;

function doCalculate() {

Additional2 = document.getElementById('Additional2').value;
RegistrationFee = document.getElementById('RegistrationFee').value;

if(Additional2 != '') {
Additional2 = Additional2 * 150;
temp1 = Additional2;
} else { temp1 = 0; }

if(RegistrationFee != '') {
RegistrationFee = RegistrationFee * 1;
temp2 = RegistrationFee;
} else { temp2 = 0; }

var total = temp1 + temp2;
document.getElementById('total').value = total;
}

</script>


This is the portion of the form as I modified it:

<td colspan="2" align="right" valign="top" nowrap="nowrap"><div align="left"><strong>I would like
<input type="text" name="Additional2" id="Additional2" size="5" onblur="doCalculate()" />
additional tickets to the Monday Awards Dinner ($150 each).</strong></div></td>
</tr>

<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><div align="right"><strong>Tuesday Lunch<br />
July 1, 2008:</strong></div></td>
<td valign="top"><input type="checkbox" name="Lunch2" value="" /></td>
</tr>

<tr valign="baseline">
<td colspan="2" align="right" valign="top" nowrap="nowrap" class="style1"><br />
<div align="left">Fees and Payment information:</div></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><div align="right"><strong>Registration Fee:</strong></div></td>
<td valign="top"><table>
<td><input type="radio" id="RegistrationFee" name="RegistrationFee" value="995" onblur="doCalculate()" />
Main registration on or before Jun 5, 2008: <strong>$995</strong></td>
</tr>
<tr>
<td><input type="radio" id="RegistrationFee" name="RegistrationFee" value="495" onblur="doCalculate()" />
Non-Profit Rate: <strong>$495</strong></td>
</tr>
<tr>
<td><input type="radio" id="RegistrationFee" name="RegistrationFee" value="250" onblur="doCalculate()" />
Student Rate: <strong>$250</strong></td>
</tr>

</table></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><strong>Total:</strong></td>
<td valign="top">$
<input type="text" name="total" id="total" readonly="readonly" /></td>
</tr>


Using the radio button causes the script to fill in the "total" box as "the value of the extra tickets plus the first radio amount of 995". So if I pick two extra tickets ($300) the total box fills in as $1295.

I prefer not to use a text box for this but rather use the radio button or I could use a checkbox.

Is there a way to modify the code so that the second item will see the different radio button values and add them in rather than always chosing the first amount in the radio group?

Thanks for your reply and help! I appreciate it!
jacinla
 
Posts: 4
Joined: Mon Mar 31, 2008 11:57 pm

Postby rangana on Wed Apr 02, 2008 4:15 am

Give this code a try:
Code: Select all
<html>
<head>
<script language="javascript">
function doCalculate()
   {
   var temp1 = 0;
   var Additional2 = document.getElementById('Additional2').value;
   var RegistrationFee1 = document.getElementById('RegistrationFee1').value;
   var RegistrationFee2 = document.getElementById('RegistrationFee2').value;
   var RegistrationFee3 = document.getElementById('RegistrationFee3').value;

   if(Additional2 != '') {
   Additional2 = Additional2 * 150;
   temp1 = Additional2;
   } else { temp1 = 0; }
   
   var total = temp1;
   document.getElementById('total').value = total;
   }
function additional(accept)
{
   var temp2 = parseInt(document.getElementById('Additional2').value);
   var dummy = temp2 * 150;
   var solve = dummy + parseInt(accept);
   document.getElementById('total').value = solve;
   
}
</script>

</head>
<body>
<table>
<tr>
<td colspan="2" align="right" valign="top" nowrap="nowrap"><div align="left"><strong>I would like
<input type="text" name="Additional2" id="Additional2" size="5" onblur="doCalculate()"/>
additional tickets to the Monday Awards Dinner ($150 each).</strong></div></td>
</tr>

<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><div align="right"><strong>Tuesday Lunch<br />
July 1, 2008:</strong></div></td>
<td valign="top"><input type="checkbox" name="Lunch2" value="" /></td>
</tr>

<tr valign="baseline">
<td colspan="2" align="right" valign="top" nowrap="nowrap" class="style1"><br />
<div align="left">Fees and Payment information:</div></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><div align="right"><strong>Registration Fee:</strong></div></td>
<td valign="top"><table>
<td><input type="radio" id="RegistrationFee1" name="RegistrationFee" value="995" onclick="additional(this.value)" />
Main registration on or before Jun 5, 2008: <strong>$995</strong></td>
</tr>
<tr>
<td><input type="radio" id="RegistrationFee2" name="RegistrationFee" value="495" onclick="additional(this.value)" />
Non-Profit Rate: <strong>$495</strong></td>
</tr>
<tr>
<td><input type="radio" id="RegistrationFee3" name="RegistrationFee" value="250" onclick="additional(this.value)" />
Student Rate: <strong>$250</strong></td>
</tr>

</table></td>
</tr>
<tr valign="baseline">
<td align="right" valign="top" nowrap="nowrap"><strong>Total:</strong></td>
<td valign="top">$
<input type="text" name="total" id="total" readonly="readonly" /></td>
</tr>

</table>
</body>
</html>


I don't know what's the purpose of your checkbox. But basically, that code should keep you going ;)

See if it helps ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby jacinla on Wed Apr 02, 2008 3:12 pm

Hi!

Thank you so much :D for taking time to do that. The code works great if both fields are used (guest tickets and registration) but if I do registration only the total fills with "NaN". Can it be set up so it can just be registration only as some folks won't buy extra guest tickets?

You can forget about the checkbox... it's in a different area now.

Thanks again for your kindness!!!!!!
jacinla
 
Posts: 4
Joined: Mon Mar 31, 2008 11:57 pm

Postby rangana on Wed Apr 02, 2008 11:52 pm

Query the value of 'Nan' ;)

You can use isNan method ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines

Postby jacinla on Thu Apr 03, 2008 11:29 pm

Great. Thanks again for all your help and support!
jacinla
 
Posts: 4
Joined: Mon Mar 31, 2008 11:57 pm

Postby rangana on Fri Apr 04, 2008 2:19 am

No problem, Glad I could help ;)
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 4 guests