SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple If statement?

    I am sure this is pretty basic, but I can't seem to get it to work. I have two form fields. One is calculated discount using some javascript, that works fine. The other is calculated profit off of that first field. I want the profit field to be calculated and populated on screen any time there is a value in the first field. So, when the page is new and blank, nothing shows, but once the discount field has been calculated the profit will show anytime you open that page.

    Simple right? Can someone show me the code i need to make that happen?

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Calculate and display it along with the discount?
    Saul

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, it's the sales price field, but yes. If there is a value in the sales price field i want the profit to be calculated and displayed in the form field.

    I had this all working using a submit button that would make the profit calculation when clicked, but it's not saved when the page is closed and user doesn't want to have to reclick the submit to view the profit each time.

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I mean you can calculate the value at the same time when you do the discount.
    Saul

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just uncertain how to write the IF statement to do it at the same time the other is calculated.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You don't really need any ifs. Just add an extra calculation to your existing function:
    Code JavaScript:
    function calculateDiscount(){
      //calculate discount
      //...
     
      calculateProfit();
    }
     
    function calculateProfit(){
      //calculate profit
    }
    Saul

  7. #7
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have that now.

    function calc_discount(){
    price = document.mainform.vendorlistprice.value;
    price = price.replace(/[^\d\.]/g,"");
    discnt = document.mainform.discount.value;
    discnt = discnt.replace(/[^\d\.]/g,"");
    reducedprice = price*(discnt/100);
    document.mainform.sale_price.value = (price-reducedprice).toFixed(2);
    }
    function calc_second_reduction(){
    price = document.mainform.vendorlistprice.value;
    price = price.replace(/[^\d\.]/g,"");
    discnt = document.mainform.stb_discount.value;
    discnt = discnt.replace(/[^\d\.]/g,"");
    reducedprice = price*(discnt/100);
    document.mainform.cogs.value = (price-reducedprice).toFixed(2);
    }
    function calc_profit(){
    saleprice = document.mainform.sale_price.value
    saleprice = saleprice.replace(/[^\d\.]/g,"");
    cogs = document.mainform.cogs.value
    cogs = cogs.replace(/[^\d\.]/g,"");
    document.mainform.profit.value = (saleprice-cogs).toFixed(2);
    }

    But each requires a submit button to calc.
    I tried adding the profit function in with the other and it won't work.
    Really I need the profit to fire if there is a value in the saleprice and cogs field as you see in my code.

  8. #8
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, I see you have put the calc_profit function in the bottom of the other function. let me try that.


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
  •