Results 1 to 8 of 8

Thread: Automatic Daily Price Reduction Script?

  1. #1
    Join Date
    Apr 2010
    Location
    West Sussex, England
    Posts
    5
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Automatic Daily Price Reduction Script?

    I would like to display a price on my website that decreases by a set amount each day. I would assign values to ‘StartDate’, ‘StartPrice’ and ‘ReductionAmount’. For example, if StartDate=(2010,3,1), StartPrice=2000 and ReductionAmount=50.

    Then, hopefully, (StartPrice – ((Date – StartDate) x ReductionAmount)) should give the following:

    On April 1 2010, website showed “Price today is $2000

    On April 2 2010 website shows “Price today is $1950

    On April 3 2010 website will show “Price today is $1900

    etc ...

    As I am an embarrassing novice, could you please help me with the script for this! Thank you very much.

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function Reduction(o){
     var srt=o.StartDate.split(/\D/);
     var days=Math.floor((new Date()-new Date(srt[0]*1,srt[1]-1,srt[2]*1,0,0,0,0))/1000/60/60/24);
     var cal=o.StartPrice-days*o.ReductionAmount;
     if (cal>0){
      var obj=document.getElementById(o.ID);
      obj[obj.nodeName=='INPUT'?'value':'innerHTML']='$'+cal;
     }
    }
    
    /*]]>*/
    </script>
    
    </head>
    
    <body>
    <input id="t1" />
    <br />
    <span id="t2" ></span>
    
    <script  type="text/javascript">
    /*<![CDATA[*/
    
    Reduction({
     ID:'t1',
     StartDate:'2010/4/1',
     StartPrice:2000,
     ReductionAmount:50
    });
    
    Reduction({
     ID:'t2',
     StartDate:'2010-3-1',
     StartPrice:1000,
     ReductionAmount:10
    });
    
    /*]]>*/
    </script>
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. The Following User Says Thank You to vwphillips For This Useful Post:

    StephenGeorge (04-05-2010)

  4. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Be aware that Javascript is not secure.

    It's best to use PHP or another serverside language (processed on the server, not on the user's computer like Javascript) when you are dealing with any "secure" information.

    There are a few reasons for this:
    1. In this case it will display it to the user. It will NOT save it for your records or display the same info on the next page (like in the shopping cart) unless you have that same script run on every page-- but it still won't have that value once they end up paying for it, unless you somehow transfer it to the server such as through a form.
    2. Javascript can be altered by the client. They can, if they know Javascript, change the price. If there is any way that means they would be able to pay less, then you would not want that. (For example, if as I said in (1), they could submit it through a form to the server, and be paying whatever they enter rather than the default.)
    3. If for nothing else, users can then view this script and figure out your pricing plans: they will wait a week and you'll look 7*$50, or something like that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. The Following User Says Thank You to djr33 For This Useful Post:

    StephenGeorge (04-05-2010)

  6. #4
    Join Date
    Apr 2010
    Location
    West Sussex, England
    Posts
    5
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thank you vwphillips and djr33 for your replies.

    Unfortunately, I don’t think I have managed to describe what I am looking for particularly well, for which I apologise!

    • I am trying to create an automatic reverse auction!
    • There will only be one instance of ‘bidding’.
    • The price will simply be displayed on the website as an ‘offer to buy’.
    • The price will start high and go down a set amount each day.
    • The start price, start date and reduction amount will all be decided by me and fixed into the Javascript coding.
    • The reduction will occur at the same time each day, when the Date officially changes, and will stay at the new lower price for the rest of the day.
    • Another reduction will occur the next day etc.
    • When someone decides they want to buy the item on offer at the price displayed on a particular day (rather than wait for the price to reduce further at the risk of losing the item to someone else with a deeper pocket!), they will email their intention to me.
    • They will not be able to buy it directly from the website, nor will they be able to change the price on the website themselves as there would be no form or facility available to do this.
    • Javascript would be just the mechanism to display the reducing price each day.
    • The price each day will be calculated by the fixed ‘variables’ set in the Javascript code (ie the start date, start price and reduction amount).
    • The only truly variable ‘variable’ will be the Date, and even that will change automatically each day!


    I hope this explains my thoughts slightly better than my first attempt!

    Thank you for your help.

  7. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Then I understand, but there is still one applicable issue from above:
    3. If for nothing else, users can then view this script and figure out your pricing plans: they will wait a week and you'll look 7*$50, or something like that.

    And also the fact that visitors without Javascript turned on will see nothing.

    I would recommend using a serverside language like PHP if your server allows it and that is very easy to setup.
    It's easy in Javascript too, but then actually getting the text to show up somewhere on the page is slightly more complex, but possible.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. The Following User Says Thank You to djr33 For This Useful Post:

    StephenGeorge (04-05-2010)

  9. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Other considerations here are that without a conversion to UT, the time/price in javascript will vary at times depending upon the user's timezone. However, if their system clock is inaccurate, even with a conversion to UT, the price they see will be off, perhaps wildly so. You would be advertising a price to some viewers that you have no intention of meeting. Depending upon circumstances, this could lead to lawsuits.

    If for no other reason, this last alone dictates that this should be done on the server side.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. The Following User Says Thank You to jscheuer1 For This Useful Post:

    StephenGeorge (04-05-2010)

  11. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's possible to seed the time valid from a serverside value (or perhaps is there a way to get this using Javascript from a timestamp on the file from the server or something?), but that seems to defeat the point anyway.

    If it is possible to do this in PHP, you can also create a database so that you don't have to setup everything manually, if you'd like.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  12. The Following User Says Thank You to djr33 For This Useful Post:

    StephenGeorge (04-05-2010)

  13. #8
    Join Date
    Apr 2010
    Location
    West Sussex, England
    Posts
    5
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default

    Thank you jscheuer1 and djr33 again! Your points about whether the user has Javascript enabled, what their system clock time is and also their time zone are critical.

    It is now obvious to me that I need to have complete control over what price is displayed on the user's screen and this can only be done server-side. Javascript is not the right way to go about this. PHP definitely seems to be the right course of action, especially factoring in the use of a database, as suggested.

    I think I will repost in the PHP forum!

    Thank you all for your help.

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
  •