Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Despite the similar sounding names, Java is not the same as Javascript.
    Moving from Java forum to Javascript forum.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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".
    Last edited by Philip M; 05-08-2012 at 04:22 PM.

    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.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •