www.webdeveloper.com
Results 1 to 4 of 4

Thread: Need help with my Javascript

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Posts
    7

    Post Need help with my Javascript

    i have this one and dont know what is wrong with my script please help me figure it out

    <html>
    <head>
    <title>Duncan's Just-In-Time Donut</title>
    <link rel="stylesheet" type="text/css" href="donuts.css" />
    <script type="text/javascript">
    function updateOrder() {
    const TAXRATE = 0.0925;
    const DONUTPRICE = 0.50;
    var numCakeDonuts = parseInt(document.getElementById("cakedonuts").value);
    var numGlazedDonuts = parseInt(document.getElementById("glazeddonuts").value);
    if (isNaN(numCakeDonuts)) {
    numCakeDonuts = 0;
    }
    if (isNaN(numGlazedDonuts)){
    numGlazedDonuts = 0;
    }
    var subTotal = (numCakeDonuts + numGlazedDonuts) * DONUTPRICE;
    var tax = subTotal * TAXRATE;
    var total = subTotal + tax;
    document.getElementById("fieldSubTotal").value = "$" + subTotal.toFixed(2);
    document.getElementById("fieldTax").value = "$" + tax.toFixed(2);
    document.getElementById("fieldTotal").value = "$" + total.toFixed(2);
    }
    function placerOrder() {
    if (document.getElementById("customername").value=="")
    Alert("I'm sorry but you must provide your name before submitting an order.");
    else if(document.getElementById("pickupminutes").value=="")||
    isNaN(document.getElementById("pickupminutes").value))
    Alert("I'm sorry but you must provie the number of minutes until pick-up" +
    " before submitting an order.");
    else
    // Submit order to server...
    form.submit();
    }
    </script>
    </head>
    <body>
    <div id="frame">
    ...
    <form name="orderform" action="donut.php" method="POST">
    <div class="field">
    Name: <input type="text" id="customerName" name="customerName"
    value="" onchange=""/>
    </div>
    <div class="field">
    # of cake donut: <input type="text" id="cakedonut" name="cakedonut"
    Value="" onchange="updateOrder();" />
    </div>
    <div class="field">
    # of glazed donut: <input type="text" id="glazeddonuts" name="glazeddonuts"
    value="" onchange="updateOrder();" />
    </div>
    <div class="field">
    Minutes to Pickup: <input type="text" id="minutes" name="minutes"
    value="" onchange=""/>
    </div>
    <div class="field">
    Subototal: <input type="text" id="fieldSubTotal" name="fieldSubTotal"
    value="" onchange="" readonly="readonly"/>
    </div>

    <div class="field">
    Tax: <input type="text" id="fieldTax" name="fieldTax"
    value="" onchange="" readonly="readonly"/>
    </div>

    <div class="field">
    Total: <input type="text" id="fieldTotal" name="fieldTotal"
    value="" onchange="" readonly="readonly"/>
    </div>
    <div class="field">
    <input type="button" value="Place Order"
    onclick="placeOrder(this.form);" />
    </div>
    </form>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,681
    Code:
    <html>
    <head>
    <title>Duncan's Just-In-Time Donut</title>
    <link rel="stylesheet" type="text/css" href="donuts.css" />
    <script type="text/javascript">
    
    var TAXRATE = 0.0925;
    var DONUTPRICE = 0.50;
    
    function updateOrder() {
     var numCakeDonuts = document.getElementById("cakedonuts");
     var numGlazedDonuts =document.getElementById("glazeddonuts");
     if (numCakeDonuts&&numGlazedDonuts){
      numCakeDonuts=parseInt(numCakeDonuts.value);
      numGlazedDonuts=parseInt(numGlazedDonuts.value);
      if (isNaN(numCakeDonuts)) {
       numCakeDonuts = 0;
      }
      if (isNaN(numGlazedDonuts)){
       numGlazedDonuts = 0;
      }
      var subTotal = (numCakeDonuts + numGlazedDonuts) * DONUTPRICE;
      var tax = subTotal * TAXRATE;
      var total = subTotal + tax;
      document.getElementById("fieldSubTotal").value = "$" + subTotal.toFixed(2);
      document.getElementById("fieldTax").value = "$" + tax.toFixed(2);
      document.getElementById("fieldTotal").value = "$" + total.toFixed(2);
     }
    }
    
    function placerOrder() {
     if (document.getElementById("customername").value==""){
      Alert("I'm sorry but you must provide your name before submitting an order.");
     }
     else if(document.getElementById("pickupminutes").value==""||isNaN(document.getElementById("pickupminutes").value)){
      alert("I'm sorry but you must provie the number of minutes until pick-up" +" before submitting an order.");
     }
     else {
    // Submit order to server...
      form.submit();
     }
    }
    </script>
    </head>
    <body>
    <div id="frame">
    ...
     <form name="orderform" action="donut.php" method="POST">
      <div class="field">
       Name: <input type="text" id="customerName" name="customerName" value="" />
      </div>
      <div class="field">
       # of cake donut: <input type="text" id="cakedonuts" name="cakedonut" value="" onkeyup="updateOrder();" />
      </div>
      <div class="field">
       # of glazed donut: <input type="text" id="glazeddonuts" name="glazeddonuts" value="" onkeyup="updateOrder();" />
      </div>
      <div class="field">
       Minutes to Pickup: <input type="text" id="minutes" name="minutes" value="" />
      </div>
      <div class="field">
       Subototal: <input type="text" id="fieldSubTotal" name="fieldSubTotal" value=""  readonly="readonly"/>
      </div>
    
      <div class="field">
       Tax: <input type="text" id="fieldTax" name="fieldTax" value=""  readonly="readonly"/>
      </div>
    
      <div class="field">
       Total: <input type="text" id="fieldTotal" name="fieldTotal" value=""  readonly="readonly"/>
      </div>
      <div class="field">
       <input type="button" value="Place Order" onclick="placeOrder(this.form);" />
      </div>
     </form>
    </div>
    
    </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. #3
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    i haven't analized the script, just fixed some brackets and id's:

    Code:
    <html>
    <head>
    <title>Duncan's Just-In-Time Donut</title>
    <link rel="stylesheet" type="text/css" href="donuts.css" />
    <script type="text/javascript">
    function updateOrder(){
    const TAXRATE = 0.0925;
    const DONUTPRICE = 0.50;
    var numCakeDonuts = parseInt(document.getElementById("cakedonuts").value);
    var numGlazedDonuts = parseInt(document.getElementById("glazeddonuts").value);
    if(isNaN(numCakeDonuts)){numCakeDonuts = 0;}
    if(isNaN(numGlazedDonuts)){numGlazedDonuts = 0;}
    var subTotal = (numCakeDonuts + numGlazedDonuts) * DONUTPRICE;
    var tax = subTotal * TAXRATE;
    var total = subTotal + tax;
    document.getElementById("fieldSubTotal").value = "$" + subTotal.toFixed(2);
    document.getElementById("fieldTax").value = "$" + tax.toFixed(2);
    document.getElementById("fieldTotal").value = "$" + total.toFixed(2);
    }
    function placeOrder() {
    if (document.getElementById("customername").value==""){alert("I'm sorry but you must provide your name before submitting an order.");}
    else if((document.getElementById("minutes").value=="") || isNaN(document.getElementById("minutes").value)){
    alert("I'm sorry but you must provie the number of minutes until pick-up" + " before submitting an order.");
    }
    else{
    // Submit order to server...
    document.forms['orderform'].submit();
    }
    }
    </script>
    </head>
    <body>
    <div id="frame">
    ...
    <form name="orderform" action="donut.php" method="post">
    <div class="field">Name: <input type="text" id="customername" name="customername" value="" onchange="" /></div>
    <div class="field"># of cake donut: <input type="text" id="cakedonut" name="cakedonut" value="" onchange="updateOrder();" /></div>
    <div class="field"># of glazed donut: <input type="text" id="glazeddonuts" name="glazeddonuts"  value="" onchange="updateOrder();" /></div>
    <div class="field">Minutes to Pickup: <input type="text" id="minutes" name="minutes" value="" onchange="" /></div>
    <div class="field">Subototal: <input type="text" id="fieldSubTotal" name="fieldSubTotal" value="" onchange="" readonly="readonly" /></div>
    <div class="field">Tax: <input type="text" id="fieldTax" name="fieldTax" value="" onchange="" readonly="readonly" /></div>
    <div class="field">Total: <input type="text" id="fieldTotal" name="fieldTotal" value="" onchange="" readonly="readonly" /></div>
    <div class="field"><input type="button" value="Place Order" onclick="placeOrder(this.form);" /></div>
    </form>
    </div>
    </body>
    </html>
    oops i'm late lol
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  4. #4
    Join Date
    Jan 2013
    Posts
    7
    it not late lol it actualy help me thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles