SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript if Statement Help for Discounts

    Hi everyone,

    I have a problem that maybe someone could help me with.

    I'm creating a order form script for a client where the user can fill out an enquiry form that contains a simple calculator. So the user will be able to type in name, address, email etc.

    As well as this there are four text boxes saying to fill out a reference number of the product they are enquiring about. Then a text box for quantity, a text box for price, and a text box for the total.

    This is repeated 3 times and then there is a text box for a grand total. So by using JavaScript it automatically generates a grand total depending on what they fill out. This is the JavaScript code is created:

    Code:
    function calculate()
    {
    
       QtyA = 0; QtyB = 0; QtyC = 0;
       TotA = 0; TotB = 0; TotC = 0;
       PrcA = 0; PrcB = 0; PrcC = 0; 
       
       //Below the code for the price
       
       if (document.ofrm.prcA.value > "")
          { PrcA = document.ofrm.prcA.value };
       document.ofrm.prcA.value = eval(PrcA);
       
       if (document.ofrm.prcB.value > "")
          { PrcB = document.ofrm.prcB.value };
       document.ofrm.prcB.value = eval(PrcB);
       
       if (document.ofrm.prcC.value > "")
          { PrcC = document.ofrm.prcC.value };
       document.ofrm.prcC.value = eval(PrcC);
       
       //Below the code for the quanity
       
       if (document.ofrm.qtyA.value > "")
          { QtyA = document.ofrm.qtyA.value };
       document.ofrm.qtyA.value = eval(QtyA);
    
       if (document.ofrm.qtyB.value > "")
          { QtyB = document.ofrm.qtyB.value };
       document.ofrm.qtyB.value = eval(QtyB);
    
       if (document.ofrm.qtyC.value > "")
          { QtyC = document.ofrm.qtyC.value };
       document.ofrm.qtyC.value = eval(QtyC);
    	
       //Get the totals for the calculator (May need to be altered)
       
       TotA = QtyA * PrcA;
       document.ofrm.totalA.value = dm(eval(TotA));
    
       TotB = QtyB * PrcB;
       document.ofrm.totalB.value = dm(eval(TotB));
    
       TotC = QtyC * PrcC;
       document.ofrm.totalC.value = dm(eval(TotC));
    
    
       Totamt =
          eval(TotA) +
          eval(TotB) +
          eval(TotC) ;
    
       document.ofrm.GrandTotal.value = dm(eval(Totamt));
    }
    What I'm trying to do now is. Having a small discount script. so if the total is between

    55-129 the user will get 10% discount
    130-249 the user will get 15% discount
    250+ the user will get 25% discount.

    I hope someone can help me with this thanks.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,677
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by barrycorrigan View Post
    What I'm trying to do now is.
    Please for gods sake do not do this with javascript. Otherwise you will find people modifying their submitted form to give them a 120% discount. Javascript and the submitted forms are easily modified by people on the client-side.

    Instead, use AJAX to send the form data ahead-of-time to the server, so that the server can respond with the discount to show the person.

    When the form is submitted, the discount is worked out by a separate piece of server-side code. That same server-side code can then be used by the AJAX process used to retrieve the discount info.

    The discount must not be sent with the form, because then there is room for the submitted data to be modified and screwed about by the client.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thanks for the Tip,

    Any ideas on the syntax for this. I'm not great with if and else statements. The ones in the code are from other sites.

    Regards

    Barry


Tags for this Thread

Bookmarks

Posting Permissions

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