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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    New User Looking for help with applying a discount in to a value based on user input

    Trying to apply a discount to the calculate function. I can't seem to get the function to work when adding the function I have started to make with a if else layout comparing the form fields with an if statement giving the discount value. Any help would be great.

    Code:
    /*
    This source is shared under the terms of LGPL 3
    www.gnu.org/licenses/lgpl.html
    
    You are free to use the code in Commercial or non-commercial projects
    */
     var papersize_prices = new Array();
     papersize_prices["6.25x9"]=.25;
     papersize_prices["6.25x11"]=.33;
     papersize_prices["4x12"]=.25;
     papersize_prices["8.5x7"]=.50;
     papersize_prices["8.5x11"]=.50;
     papersize_prices["12x15"]=1;
     
    
    
     //Set up an associative array
     //The keys represent the size of the cake
     //The values represent the cost of the cake i.e A 10" cake cost's $35
     var cake_prices = new Array();
     cake_prices["100"]=100;
     cake_prices["250"]=250;
     cake_prices["500"]=500;
     cake_prices["1000"]=1000;
     cake_prices["2000"]=2000;
     cake_prices["5000"]=5000;
     cake_prices["7000"]=7000;
     cake_prices["10000"]=10000;
     cake_prices["15000"]=15000;
     cake_prices["20000"]=20000;
     cake_prices["30000"]=30000;
     cake_prices["50000"]=50000;
      
     
     //Set up an associative array 
     //The keys represent the filling type
     //The value represents the cost of the filling i.e. Lemon filling is $5,Dobash filling is $9
     //We use this this array when the user selects a filling from the form
     var filling_prices= new Array();
     filling_prices["None"]=0;
     filling_prices["80lb_Cover"]=1;
     filling_prices["100lb_Cover"]=2;
     filling_prices["120lb_Cover"]=3;
     filling_prices["80lb_Gloss/Matted"]=4;
     filling_prices["100lb_Gloss/Matted"]=5;
     filling_prices["120lb_Gloss/Matted"]=6;
     
     var mail_prices= new Array();
     mail_prices["None"]=0;
     mail_prices["EDDMREADY"]=10;
     
     
     
    
    
    function getCakeSizePrice()
    {
        var cakeSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedCake = theForm.elements["selectedcake"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	cakeSizePrice = cake_prices[selectedCake.value];
        //finally we return cakeFillingPrice
        return cakeSizePrice;
    }
    
    function getPaperSizePrice()
    {
        var paperSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedPaper = theForm.elements["PaperSize"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	paperSizePrice = papersize_prices[selectedPaper.value];
        //finally we return cakeFillingPrice
        return paperSizePrice;
    }
    
    function getdiscount()
    {
     var discount = 0;
     var selectedPaperSize = theForm.elements["PaperSize"];
     paperSizePrice = papersize_prices[selectedPaperSize.value];
     var selectedCake = theForm.elements["selectedcake"];
     cakeSizePrice = cake_prices[selectedCake.value];
     var selectedFilling = theForm.elements["filling"];
     cakeFillingPrice = filling_prices[selectedFilling.value];
     
     if (paperSizePrice ="500")
     {
    	 if (cakeSizePrice="6.25x9")
    	 {
    		 if (cakeFillingPrice="80lb_Cover")
    		 discount =".20";
    	 }
     return discount;
     }
    }
    //This function finds the filling price based on the 
    //drop down selection
    function getFillingPrice()
    {
        var cakeFillingPrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="filling"
         var selectedFilling = theForm.elements["filling"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
        cakeFillingPrice = filling_prices[selectedFilling.value];
    
        //finally we return cakeFillingPrice
        return cakeFillingPrice;
    }
    
    function getoption1price()
    {
        var option1price=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="option1"
         var option1price = theForm.elements["mail"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	option1price = mail_prices[option1price.value];
        //finally we return cakeFillingPrice
        return option1price;
    }
    
    //candlesPrice() finds the candles price based on a check box selection
    function candlesPrice()
    {
        var candlePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includecandles"
        var includeCandles = theForm.elements["includecandles"];
    
        //If they checked the box set candlePrice to 5
        if(includeCandles.checked==true)
        {
            candlePrice=5;
        }
        //finally we return the candlePrice
        return candlePrice;
    }
    
    function insciptionPrice()
    {
        //This local variable will be used to decide whether or not to charge for the inscription
        //If the user checked the box this value will be 20
        //otherwise it will remain at 0
        var inscriptionPrice=0;
        //Get a refernce to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includeinscription"
        var includeInscription = theForm.elements["includeinscription"];
        //If they checked the box set inscriptionPrice to 20
        if(includeInscription.checked==true){
            inscriptionPrice=20;
        }
        //finally we return the inscriptionPrice
        return inscriptionPrice;
    }
            
    function calculateTotal()
    {
        //Here we get the total price by calling our function
        //Each function returns a number so by calling them we add the values they return together
        var cakePrice = getPaperSizePrice() * getCakeSizePrice() * getFillingPrice() + candlesPrice() + insciptionPrice() + getoption1price();
    
        
        //display the result
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='block';
        divobj.innerHTML = "Total Price For the Order $"+cakePrice;
    
    }
    
    function hideTotal()
    {
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='none';
    }

    Form page

    Code:
    <form action="" id="cakeform" onsubmit="return false;">
            <div>
                <div class="cont_order">
                   <fieldset>
                    <legend>Make your EDDM Order!</legend>
                    <label >Size Of the Order</label>
    
              <label>Paper Size</label>
                       <select id="PaperSize" name='PaperSize' onchange="calculateTotal()">
                    <option value="None">Select PaperStock</option>
                    <option value="6.25x9">6.25" x 9"</option>
                    <option value="6.25x11">6.25" x 11"</option>
                    <option value="4x12">4" x 12"</option>
                    <option value="8.5x7">8.5" x 7"</option>
                    <option value="8.5x11">8.5" x 11"</option>
                    <option value="12x15">12" x 15"</option>
                   </select>
             <label>QTY</label>
                    <select id="selectedcake" name='selectedcake' onchange="calculateTotal()">
                    <option value="None">Select qty</option> 
                    <option value="500">500</option>
                    <option value="1000">1000</option>
                    <option value="2000">2000</option>
                    <option value="5000">5000</option>
                    <option value="7500">7500</option>
                    <option value="10000">10000</option>
                    <option value="15000">15000</option>
                    <option value="20000">20000</option>
                    <option value="30000">30000</option>
                    <option value="50000">50000</option>
                   </select>
                    <label for="QTY"></label>
                    <label >Paper Stock</label>
             
                    <select id="filling" name='filling' onchange="calculateTotal()">
                    <option value="None">Select PaperStock</option>
                    <option value="80lb_Cover">80lb Cover</option>
                    <option value="100lb_Cover">100lb Cover</option>
                    <option value="120lb_Cover">120lb Cover</option>
                    <option value="80lb_Gloss/Matted">80lb Gloss/Matted</option>
                    <option value="100lb_Gloss/Matted">100lb Gloss/Matted</option>
                    <option value="120lb_Gloss/Matted">120lb Gloss/Matted</option>
                   </select>
                        <label >Mail Option</label>
             
                    <select id="mail" name='mail' onchange="calculateTotal()">
                    <option value="None">Select Mail Option</option>
                    <option value="None">customer to handling shipping</option>
                    <option value="EDDMREADY">EDDM Ready Drop OFF</option>
                   </select><label for="mail"></label>
                   
                    <br/>
                    <p>
                    <label for='includecandles' class="inlinelabel">Include Candles($5)</label>
                   <input type="checkbox" id="includecandles" name='includecandles' onclick="calculateTotal()" />
                   </p>
                   
                    <p>
                    <label class="inlinelabel" for='includeinscription'>Include Inscription($20)</label>
                    <input type="checkbox" id="includeinscription" name="includeinscription" onclick="calculateTotal()" />
                    <input type="text"  id="theinscription" name="theinscription" value="Enter Inscription"  />
                    </p>
                    <div id="totalPrice"></div>
                    
                    </fieldset>
                </div>
                
            	<div class="cont_details">
                	<fieldset>
                    <legend>Contact Details</legend>
                    <label for='name'>Name</label>
                    <input type="text" id="name" name='name' />
                    <br/>
                    <label for='address'>Address</label>
                    <input type="text" id="address" name='address' />
                    <br/>
                    <label for='phonenumber'>Phone Number</label>
                    <input type="text"  id="phonenumber" name='phonenumber'/>
                    <br/>
                    </fieldset>
                </div>
                <input type='submit' id='submit' value='Submit' onclick="calculateTotal()" />
            </div>  
           </form>

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Wouldn't it be better to use *MEANINGFUL* names. Ones relevant to YOUR project.

    Instead of "selectedcake" and "cakeprices" when what you really *MEAN* is simply "quantity". Oh, and the "cakeprices" associative array is a total waste of code.

    And why would you include candles on an order for paper? Or an inscription?

    How about *FIRST* producing a <form> and HTML code that makes sense, THEN worry about putting the JavaScript code behind it.
    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.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    And by the by... This code isn't wrong:
    Code:
     var papersize_prices = new Array();
     papersize_prices["6.25x9"]=.25;
     papersize_prices["6.25x11"]=.33;
     papersize_prices["4x12"]=.25;
     papersize_prices["8.5x7"]=.50;
     papersize_prices["8.5x11"]=.50;
     papersize_prices["12x15"]=1;
    But it can be written *SO* much more simply:
    Code:
    var papersize_prices = {
         "6.25x9" : .25,
         "6.25x11 : .33,
         "4x12" : .25,
         "8.5x7" : .50,
         "8.5x11" : .50,
         "12x15" : 1
    };
    It might help to not copy ancient ancient code that probably wasn't worth copying in the first place.
    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.

  • #4
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If the task at hand is not any closer to the end result, I am not going to waist time with changing things that are not my concern as of right now. This code was pulled from someone else site.I was using this code for my base of making this calc work. This is my first javascript I have really tried to mod to do something more. Please forgive me for not being up to date with the newest programming trends for javascript.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Starting with that other code is fine, but one more time: How are "candles" and "inscription" relevant to paper pricing?

    Steal the concepts, fine. But don't clone variable and form field names that make no sense in your application.

    You will pardon me for saying this, I hope, but this "smells" like homework for a class. I know that cake pricing code is straight out of a (rather old-fashioned and out-of-date) text book.

    And if it is homework, then see rule 1.5 in http://codingforums.com/rules.htm

    If it is not homework--if it really is for commercial use--then all the more reason it shouldn't be using candles when calculating paper prices.
    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.

  • #6
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    almost there

    Code:
      <form action="" id="cakeform" onsubmit="return false;">
            <div>
                <div class="cont_order">
                   <fieldset>
                    <legend>Make your EDDM Order!</legend>
                    <label >Size Of the Order</label>
    
              <label>Paper Size</label>
                       <select id="PaperSize" name='PaperSize' onchange="calculateTotal()">
                    <option value="None">Select PaperStock</option>
                    <option value="6.25x9">6.25" x 9"</option>
                    <option value="6.25x11">6.25" x 11"</option>
                    <option value="4x12">4" x 12"</option>
                    <option value="8.5x7">8.5" x 7"</option>
                    <option value="8.5x11">8.5" x 11"</option>
                    <option value="12x15">12" x 15"</option>
                    <option value=1>Testing1</option>
                    <option value=2>Testing2</option>
                   </select>
             <label>QTY</label>
                    <select id="selectedcake" name='selectedcake' onchange="calculateTotal()">
                    <option value="None">Select qty</option> 
                    <option value="500">500</option>
                    <option value="1000">1000</option>
                    <option value="2000">2000</option>
                    <option value="5000">5000</option>
                    <option value="7500">7500</option>
                    <option value="10000">10000</option>
                    <option value="15000">15000</option>
                    <option value="20000">20000</option>
                    <option value="30000">30000</option>
                    <option value="50000">50000</option>
                    <option value=1>testing1</option>
                    <option value=2>testing2</option>
                   </select>
                   
                    <label >Paper Stock</label>
             
                    <select id="paperstock" name='paperstock' onchange="calculateTotal()">
                    <option value="None">Select PaperStock</option>
                    <option value="80lb_Cover">80lb Cover</option>
                    <option value="100lb_Cover">100lb Cover</option>
                    <option value="120lb_Cover">120lb Cover</option>
                    <option value="80lb_Gloss/Matted">80lb Gloss/Matted</option>
                    <option value="100lb_Gloss/Matted">100lb Gloss/Matted</option>
                    <option value="120lb_Gloss/Matted">120lb Gloss/Matted</option>
                    <option value=1>testing1</option>
                    <option value=2>testing2</option>
                   </select>
                        <label >Mail Option</label>
             
                    <select id="mail" name='mail' onchange="calculateTotal()">
                    <option value="None">Select Mail Option</option>
                    <option value="None">customer to handling shipping</option>
                    <option value="EDDMREADY">EDDM Ready Drop OFF</option>
                   </select><label for="mail"></label>
                   
                    <br/>
                    <p>
                    <label for='includecandles' class="inlinelabel">Include Candles($5)</label>
                   <input type="checkbox" id="includecandles" name='includecandles' onclick="calculateTotal()" />
                   </p>
                   
                    <p>
                    <label class="inlinelabel" for='includeinscription'>Include Inscription($20)</label>
                    <input type="checkbox" id="includeinscription" name="includeinscription" onclick="calculateTotal()" />
                    <input type="text"  id="theinscription" name="theinscription" value="Enter Inscription"  />
                    </p>
                    <div id="totalPrice"></div>
                    
                    </fieldset>
                </div>
                
            	<div class="cont_details">
                	<fieldset>
                    <legend>Contact Details</legend>
                    <label for='name'>Name</label>
                    <input type="text" id="name" name='name' />
                    <br/>
                    <label for='address'>Address</label>
                    <input type="text" id="address" name='address' />
                    <br/>
                    <label for='phonenumber'>Phone Number</label>
                    <input type="text"  id="phonenumber" name='phonenumber'/>
                    <br/>
                    </fieldset>
                </div>
                <input type='submit' id='submit' value='Submit' onclick="calculateTotal()" />
            </div>  
           </form>
    js code

    Code:
    /*
    This source is shared under the terms of LGPL 3
    www.gnu.org/licenses/lgpl.html
    
    You are free to use the code in Commercial or non-commercial projects
    */
     var papersize_prices = new Array();
     papersize_prices["6.25x9"]=.25;
     papersize_prices["6.25x11"]=.33;
     papersize_prices["4x12"]=.25;
     papersize_prices["8.5x7"]=.50;
     papersize_prices["8.5x11"]=.50;
     papersize_prices["12x15"]=1;
     papersize_prices[1]=.25;
     papersize_prices[2]=.33;
    
    
     //Set up an associative array
     //The keys represent the size of the cake
     //The values represent the cost of the cake i.e A 10" cake cost's $35
     var cake_prices = new Array();
     cake_prices["500"]=500;
     cake_prices["1000"]=1000;
     cake_prices["2000"]=2000;
     cake_prices["5000"]=5000;
     cake_prices["7000"]=7000;
     cake_prices["10000"]=10000;
     cake_prices["15000"]=15000;
     cake_prices["20000"]=20000;
     cake_prices["30000"]=30000;
     cake_prices["50000"]=50000;
     cake_prices[1]=500;
     cake_prices[2]=1000;
     
     //Set up an associative array 
     //The keys represent the filling type
     //The value represents the cost of the filling i.e. Lemon filling is $5,Dobash filling is $9
     //We use this this array when the user selects a filling from the form
     var paperstock_prices= new Array();
     paperstock_prices["None"]=0;
     paperstock_prices["80lb_Cover"]=.01;
     paperstock_prices["100lb_Cover"]=.01;
     paperstock_prices["120lb_Cover"]=.02;
     paperstock_prices["80lb_Gloss/Matted"]=.01;
     paperstock_prices["100lb_Gloss/Matted"]=.01;
     paperstock_prices["120lb_Gloss/Matted"]=.02;
     paperstock_prices[1]=.01;
     paperstock_prices[2]=.02;
    
     var mail_prices= new Array();
     mail_prices["None"]=0;
     mail_prices["EDDMREADY"]=10;
     
     
     
    function getCakeSizePrice()
    {
        var cakeSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedCake = theForm.elements["selectedcake"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	cakeSizePrice = cake_prices[selectedCake.value];
        //finally we return cakeFillingPrice
        return cakeSizePrice;
    }
    
    function getPaperSizePrice()
    {
        var paperSizePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="qty"
         var selectedPaper = theForm.elements["PaperSize"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	paperSizePrice = papersize_prices[selectedPaper.value];
        //finally we return cakeFillingPrice
        return paperSizePrice;
    }
    
    function getdiscount()
    {
     var discount = 0;
     
     if(papersize_prices["6.25x9"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
     	discount ="0";
     if(papersize_prices["6.25x11"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
     	discount =".10";
     if(papersize_prices["4x12"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
     	discount =".20";
    	 if(papersize_prices[1] && cake_prices[1] && paperstock_prices[1])
     	discount =".50";
    if(papersize_prices[1] && cake_prices[2] && paperstock_prices[1])
     	discount =".100";
    
     return discount;
    }
    //This function finds the filling price based on the 
    //drop down selection
    function getPaperStockPrice()
    {
        var paperStockPrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="filling"
         var selectedPaperStock = theForm.elements["paperstock"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
        paperStockPrice = paperstock_prices[selectedPaperStock.value];
    
        //finally we return cakeFillingPrice
        return paperStockPrice;
    }
    
    function getoption1price()
    {
        var option1price=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the select id="option1"
         var option1price = theForm.elements["mail"];
         
        //set cakeFilling Price equal to value user chose
        //For example filling_prices["Lemon".value] would be equal to 5
    	option1price = mail_prices[option1price.value];
        //finally we return cakeFillingPrice
        return option1price;
    }
    
    //candlesPrice() finds the candles price based on a check box selection
    function candlesPrice()
    {
        var candlePrice=0;
        //Get a reference to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includecandles"
        var includeCandles = theForm.elements["includecandles"];
    
        //If they checked the box set candlePrice to 5
        if(includeCandles.checked==true)
        {
            candlePrice=5;
        }
        //finally we return the candlePrice
        return candlePrice;
    }
    
    function insciptionPrice()
    {
        //This local variable will be used to decide whether or not to charge for the inscription
        //If the user checked the box this value will be 20
        //otherwise it will remain at 0
        var inscriptionPrice=0;
        //Get a refernce to the form id="cakeform"
        var theForm = document.forms["cakeform"];
        //Get a reference to the checkbox id="includeinscription"
        var includeInscription = theForm.elements["includeinscription"];
        //If they checked the box set inscriptionPrice to 20
        if(includeInscription.checked==true){
            inscriptionPrice=20;
        }
        //finally we return the inscriptionPrice
        return inscriptionPrice;
    }
            
    function calculateTotal()
    {
        //Here we get the total price by calling our function
        //Each function returns a number so by calling them we add the values they return together
        var basepreprice =  getCakeSizePrice() * (getPaperSizePrice() + getPaperStockPrice()); 
    	
    	var orderdiscount = basepreprice * getdiscount();
    	var cakePrice =  basepreprice - orderdiscount + candlesPrice() + insciptionPrice() + getoption1price();
    
        
        //display the result
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='block';
        divobj.innerHTML = "Total Price For the Order $"+cakePrice;
    
    }
    
    function hideTotal()
    {
        var divobj = document.getElementById('totalPrice');
        divobj.style.display='none';
    }

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Better, but why do you still use names like "cake_prices" and "getCakeSizePrices"?

    Anyway, your getdiscount() code is all wrong.

    Explanation:
    Code:
     if(papersize_prices["6.25x9"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
    The expression papersize_prices["...anything..."] is asking JavaScript to LOOK UP in the papersize_prices collection what the value matching ...anything... is.

    So, in this case papersize_prices["6.25x9"] will do the look up and give you a value of .25

    Same for the lookups of cake_prices["500"] and paperstock_prices["80lb_Cover"].

    So this line
    Code:
     if(papersize_prices["6.25x9"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
    ends up being evaluated as
    Code:
     if ( .25 && 500 && .01 )
    And because of the nature of the && operator, that is *ALWAYS* true.

    As are the next two if tests in that function. The last two if tests are pure nonsense. Meaningless. paper_prices[1] doesn't exist, so its value is null, so the last two if tests are always false.

    And so that function will always end up returning a discount value of .20 (or, actually, ".20"...a string when what should be returned is a number).

    Try again. Toss that function completely and rewrite.
    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.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    Here...my rewrite of all that ugly (and very ancient and poorly-written in the original) JS coding. No code in here for calculating discount, because I don't know what the discount(s) is/are based on. Can't tell from your code.
    Code:
    <!DOCTYPE html>
    <html>
    <body>
    <form action="" id="orderform" onsubmit="return false;">
    <div>
        <div class="cont_order">
            <fieldset>
            <legend>Make your EDDM Order!</legend>
            <label >Size Of the Order</label>
    
            <label>Paper Size</label>
            <select name="papersize">
               <option value="">Select PaperStock</option>
               <option>6.25" x 9"</option>
               <option>6.25" x 11"</option>
               <option>4" x 12"</option>
               <option>8.5" x 7"</option>
               <option>8.5" x 11"</option>
               <option>12" x 15"</option>
            </select>
    
            <label>QTY</label>
            <select name="quantity">
               <option value="">Select qty</option> 
               <option>500</option>
               <option>1000</option>
               <option>2000</option>
               <option>5000</option>
               <option>7500</option>
               <option>10000</option>
               <option>15000</option>
               <option>20000</option>
               <option>30000</option>
               <option>50000</option>
            </select>
               
            <label >Paper Stock</label>
            <select name="paperstock">
               <option value="">Select PaperStock</option>
               <option>80lb Cover</option>
               <option>100lb Cover</option>
               <option>120lb Cover</option>
               <option>80lb Gloss/Matted</option>
               <option>100lb Gloss/Matted</option>
               <option>120lb Gloss/Matted</option>
            </select>
            <label >Mail Option</label>
            <select name="mail">
               <option value="None">Select Mail Option</option>
               <option value="None">customer to handling shipping</option>
               <option value="EDDMREADY">EDDM Ready Drop OFF</option>
            </select><label for="mail"></label>
            <br/>
            <div id="totalPrice">Order incomplete</div>
            
            </fieldset>
        </div>
        <input type="submit" name="submitform" value="Submit" />
    </div>  
    </form>
    
    <script type="text/javascript">
    (
      function( )
      {
          var form = document.getElementById("orderform");
          var tprice = document.getElementById("totalPrice");
    
          form.papersize.onchange = calculateTotal;
          form.quantity.onchange = calculateTotal;
          form.paperstock.onchange = calculateTotal;
          form.mail.onchange = calculateTotal;
    
          // look up tables:
          var papersize_prices = {
             "6.25x9"  : 0.25,
             "6.25x11" : 0.33,
             "4x12"    : 0.25,
             "8.5x7"   : 0.50,
             "8.5x11"  : 0.50,
             "12x15"   : 1.00
          };
          var paperstock_prices = {
             "80lb Cover"         : 0.01,
             "100lb Cover"        : 0.01,
             "120lb Cover"        : 0.02,
             "80lb Gloss/Matted"  : 0.01,
             "100lb Gloss/Matted" : 0.01,
             "120lb Gloss/Matted" : 0.02
          }
    
          function calculateTotal( )
          {
              var szix = form.papersize.selectedIndex;
              var qtix = form.quantity.selectedIndex;
              var stix = form.paperstock.selectedIndex;
              if ( szix == 0 || qtix == 0 || stix == 0 )
              {
                  tprice.innerHTML = "Order incomplete";
                  return;
              }
              var price = papersize_prices[form.papersize[szix].text.replace(/[\s\"]/g,"")];
              var qty   = Number(form.quantity[qtix].text); 
              var stock = paperstock_prices[form.paperstock[stix].text];
              var mailing = form.mail.value == "EDDMREADY" ? 10.00 : 0.00;
    
              var total = ( price + stock ) * qty;
    
              // here is where we would get and apply discount
    
              total += mailing;
    
              tprice.innerHTML = "$" + total.toFixed(2);
          }
      }
    )();
    </script>
    </body>
    </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.

  • #9
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I am not sure how to do this another way, the if(x&&y&&z) statement should not always be true because the x value is changing based on the input of the form. So the conditions in the statement would not be true because x could be 6.25x9, or 6.25x11
    Code:
    if(papersize_prices["6.25x9"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
     	discount ="0";
     if(papersize_prices["6.25x11"] && cake_prices["500"] && paperstock_prices["80lb_Cover"])
     	discount =".10";
    so if a user select 6.25x9 for the papersize and picks 500 for the cakeprice and 80lb_cover the first if statement is true the but the second if statement would not be If I am understanding this right ???

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    You don't want to LOOK UP the price of "6.25x9". You want to know *IF* that was the value chosen.

    So more like:
    Code:
        // assumes you are using my simplified <option>s, not your originals
        var psize = form.papersize[form.papersize.selectedIndex].text.replace(/[\s\"]/g,"");
        var qty    = Number( form.quantity[form.quantity.selectedIndex].text );
        var stock = form.paperstock[for.paperstock.selectedIndex].text;
    
        if ( psize == "6.25x9" && qty = 500 && stock = "80lb Cover" )
    But you need to start by making a chart of *all* the possible discounts.

    You have 6 size choices, 6 stock choices, 10 quantity choices. That's 3600 possible combinations! Surely you won't have a different discount for all 3600 of those?

    So what *WILL* the discount be based on???
    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.

  • #11
    New to the CF scene
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    At this point I could careless what I have to do to get it done!

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,118
    Thanks
    80
    Thanked 4,555 Times in 4,519 Posts
    If you can't describe the discount structure in words, then there is no possible way you will be able to convert those lack of words into code.

    Design first, *THEN* code.
    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
    •