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 2 of 2

Thread: Changing JS

  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    136
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Changing JS

    I have this script where people can pick a price and pick quataty. i want it to be only one price and let the person input quatity them selves, how can this be done?



    Thanks in advance



    Code:
    <script type="text/javascript">
    	function calculate_amount() {
    		var subtotal = 0;
    		var hamburger_subtotal = 0;
    		var hamburger = document.myform.hamburger.value;
    		var hamburger_qty = document.myform.hamburger_qty.value;
    		var cheeseburger_subtotal = 0;
    		var cheeseburger = document.myform.cheeseburger.value;
    		var cheeseburger_qty = document.myform.cheeseburger_qty.value;		
    		var pst = 0;
    		var gst = 0;
    		var total = 0;
    		//etc...
    		//var chicken_burger;
    		//var fries;
    		//var gravy;
    		//var chili;
    		if (hamburger > 0) {
    			hamburger_subtotal = hamburger * hamburger_qty;			
    		}
    		subtotal = hamburger_subtotal;
    		// myform -- depends on the name of your actual form, if it does not have one give it one.
    		document.myform.display_hamburger_subtotal.value = hamburger_subtotal;
    		if (cheeseburger > 0) {
    			cheeseburger_subtotal = cheeseburger * cheeseburger_qty;
    		}
    		// myform -- depends on the name of your actual form, if it does not have one give it one.
    		document.myform.display_cheeseburger_subtotal.value = cheeseburger_subtotal;		
    		subtotal = subtotal + cheeseburger_subtotal;
    		pst = .07 * subtotal;
    		gst = .05 * subtotal;
    		// you cannot add the values after you call toFixed, so do the total now!
    		total = subtotal + pst + gst;
    		total = total.toFixed(2);		
    		subtotal = subtotal.toFixed(2);
    		pst = pst.toFixed(2);
    		gst = gst.toFixed(2);
    		//  this is wrong var tax = foo * 1.07; 	
     
    		document.myform.display_subtotal.value = subtotal;
    		document.myform.display_pst.value = pst;
    		document.myform.display_gst.value = gst;
    		//total = subtotal += pst += gst;
     
     
    		document.myform.display_total.value = total;
     
    	}
    	</script>
    	// first off this all needs to be wrapped in form tags if you are going to post the values to something.
    	// you need to look up how to name your items, you should have an input type=x with name=y and id=y
    	<form name="myform">
    	<table width="325">
        <tbody>
        <tr>
        <th width="144">item</th>
        <th width="75">price</th>
        <th width="92">quantity</th>
        <th width="101">sub-total</th>
        </tr>
        <tr align="middle">
        <td align="left">Hamberger</td>
    			    <td><select id="hamburger" name="hamburger" onchange="calculate_amount()">
    					<OPTION VALUE='2.99'>2.99</OPTION>
    					<OPTION VALUE='3.99'>$.99</OPTION>
    					<OPTION VALUE='4.99'>4.99</OPTION>
    				</select>
    	</td>
    	 <td><select id="hamburger_qty" name="hamburger_qty" onchange="calculate_amount()">
    					<OPTION VALUE='0'>0</OPTION>
    					<OPTION VALUE='1'>1</OPTION>
    					<OPTION VALUE='2'>2</OPTION>
    					<OPTION VALUE='3'>3</OPTION>
    					<OPTION VALUE='4'>4</OPTION>
    					<OPTION VALUE='5'>5</OPTION>
    					<OPTION VALUE='6'>6</OPTION>
    					<OPTION VALUE='7'>7</OPTION>
    					<OPTION VALUE='8'>8</OPTION>
    					<OPTION VALUE='9'>9</OPTION>					
    				</select>
    	</td>
    	<td><input type="text" id="display_hamburger_subtotal" name="display_hamburger_subtotal" size="10" disabled="disabled" /></td>
        </tr>
        <td align="left">Cheeseberger</td>
    			    <td><select id="cheeseburger" name="cheeseburger" onchange="calculate_amount()">
    					<OPTION VALUE='3.99'>3.99</OPTION>
    					<OPTION VALUE='4.99'>4.99</OPTION>
    					<OPTION VALUE='5.99'>5.99</OPTION>
    				</select>
    	</td>
    	 <td><select id="cheeseburger_qty" name="cheeseburger_qty" onchange="calculate_amount()">
    					<OPTION VALUE='0'>0</OPTION>
    					<OPTION VALUE='1'>1</OPTION>
    					<OPTION VALUE='2'>2</OPTION>
    					<OPTION VALUE='3'>3</OPTION>
    					<OPTION VALUE='4'>4</OPTION>
    					<OPTION VALUE='5'>5</OPTION>
    					<OPTION VALUE='6'>6</OPTION>
    					<OPTION VALUE='7'>7</OPTION>
    					<OPTION VALUE='8'>8</OPTION>
    					<OPTION VALUE='9'>9</OPTION>					
    				</select>
    	</td>
    	<td><input type="text" id="display_cheeseburger_subtotal" name="display_cheeseburger_subtotal" size="10" disabled="disabled" /></td>
        </tr>
        <tr align="middle">
        <td align="left">Chicken Burger</td>
        <td><input size="7" value="$4.99" /></td>
        <td><input size="3" /></td>
        <td><input size="10" /></td>
        </tr>
        </tbody>
        </table>
        <table width="324">
        <tbody>
        <tr>
        <th width="124">item</th>
        <th width="42">price</th>
        <th width="72">quantity</th>
        <th width="74">sub-total</th>
        </tr>
        <tr align="middle">
        <td align="left">French Fries</td>
        <td><input size="7" value="$2.99" /></td>
        <td><input size="3" /></td>
        <td><input size="10" /></td>
        </tr>
        <tr align="middle">
        <td align="left"><input type="checkbox" />
        gravy</td>
        <td><input size="7" value="$0.50" /></td>
        <td><input size="3" /></td>
        <td><input size="10" /></td>
        </tr>
        <tr align="middle">
        <td align="left"><input type="checkbox" />
        chilli</td>
        <td><input size="7" value="$1.99" /></td>
        <td><input size="3" /></td>
        <td><input size="10" /></td>
        </tr>
        </tbody>
        </table>
        <table align="right">
        <tbody>
        <tr>
        <td>subtotal</td>
        <td><input type="text" id="display_subtotal" name="display_subtotal" size="10" disabled="disabled" /></td>
        </tr>
        <tr>
        <td>pst 7%</td>
        <td><input type="text" id="display_pst" name="display_pst" size="10" disabled="disabled" /></td>
        </tr>
        <tr>
        <td>gst 5%</td>
        <td><input type="text" id="display_gst" name="display_gst" size="10" disabled="disabled" /></td>
        </tr>
        <tr>
        <td>total</td>
        <td><input type="text" id="display_total" name="display_total" size="10" disabled="disabled" /></td>
        </tr>
        <tr>
        <td colspan="2"><input type="button" value="total up order" />
        </td>
        </tr>
        </tbody>
        </table>
    	</form>
    Last edited by VIPStephan; 12-10-2011 at 02:18 AM. Reason: wrapped code BB tags

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Ummm...don't give them a choice of price?

    If you don't want that <select> for the price, why is it there?

    There's no point in changing the JavaScript until you change the HTML.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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