Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-08-2012, 02:09 AM   PM User | #1
durkeyturkey
New to the CF scene

 
Join Date: May 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
durkeyturkey is an unknown quantity at this point
Question JavaScript for a simple order form with drop down payment

Hi everyone,
I am converting a program that displays the payment options as a list of check boxes to a drop down list. I have tried using onBlur(), etc. but the program is displaying my error that says I didn't select a payment method. Here is the code, thanks for any hints or help!
Code:
<html>
<head>
<title>Order form</title>
	<meta http-equiv="Content-Type" content="text/javascript">
	<script type="text/javascript">
	function total()
	{
		var subtotal=0;
		var total=0;
		var adjustment=1;
		payment=false;
		var elmnts=document.payform.elements;
		
		for (var x=0; x<(document.payform.elements['item[]'].length); x++)
		{
			if (document.payform.elements['item[]'][x].checked)
			{
				
				subtotal=subtotal+parseFloat(document.payform.elements['item[]'][x].value);
			}
		}

		for (var x=0; x<(document.payform.pay.length); x++)
		{
			if (document.payform.pay[x].checked)
			{
				adjustment=document.payform.pay[x].value;
				payment=true;
			}
		}
		
		if (payment)
		{
			total=subtotal*adjustment;
			total = total.toFixed(2);
			document.payform.display.value="subtotal: "+subtotal+"\rAdjustment: "+adjustment+"\rTotal: "+total;
		}
		else
		{
			window.alert("Please choose payment type.");
		}
	}
		
	</script>
</head>

<body>
	<form name="payform">
	<table border="0" cellpadding="5">
	<tr>
		<td width="250" valign="top">
		<b>Please buy some stuff!</b><br />
			<input type="checkbox" name="item[]" value="14.99" />Chang $14.99<br />
			<input type="checkbox" name="item[]" value="12.99" />Chartreuse verte $12.99<br />
			<input type="checkbox" name="item[]" value="13.99" />Gnocchi di nonna Alice $13.99<br />
			<input type="checkbox" name="item[]" value="14.99" />Gudbrandsdalsost $14.99<br /><br />
		<b>Choose payment methods</b><br />		
		<select name = "pay" size = 0>
			<option value = "0"></option>
			<option value = "1.2">Monday order (20% service charge)</option>
			<option value = "1.1">Personal check (10% service charge)</option>
			<option value = ".8">Visa (preferred -- 20% discount)</option>
			<option value = "1.2">MasterCard (10% discount)</option>
			<option value = "1.2">Discover (10% discount)</option>
		</select>	
		
		<br /><br />
		<input type="button" value="Process Order" onClick="total()" />
		<input type="reset" value="Reset Form" />
		</td></tr>
	<tr>
		<td width="200" valign="bottom">
		<textarea name="display" rows="5" cols="35"></textarea></td>
	</tr>
	</table>
	</form>
</body>
</html>
durkeyturkey is offline   Reply With Quote
Old 05-08-2012, 02:35 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Despite the similar sounding names, Java is not the same as Javascript.
Moving from Java forum to Javascript forum.
Fou-Lu is offline   Reply With Quote
Old 05-08-2012, 03:15 PM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Replace this:-

Code:
for (var x=0; x<(document.payform.pay.length); x++)
		{
			if (document.payform.pay[x].checked)
			{
				adjustment=document.payform.pay[x].value;
				payment=true;
			}
		}
with this

Code:
var si = document.payform.pay.selectedIndex;
if (si > 0) {
adjustment = document.payform.pay.value;
payment = true;
}

It is a money order, not a Monday order
and the option values of Visa and Mastercard should be .9, not 1.2.

We are sorry that as a result of a typographical error we referred to General X as "a bottle-scarred old veteran". This should of course have read "a battle-scared old veteran".
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 05-08-2012 at 03:22 PM..
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
check box, drop down list, select option

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:54 PM.


Advertisement
Log in to turn off these ads.