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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool My Brain is Blank - Simple calculate form.

    I've been staring at this computer way too long to figure this simple task out. I don't want the calculated total on this form to appear in a pop up alert window. I want a text box next to the calculate button where the figure appears.

    I know, I know!!!!! Don't quit my day job right??

    Code:
    <html>
    <head>
    <title>Calculator</title>
    <script type="text/javascript">
    var disc = [];
    //...  Example of discount code-----disc["i13_i23_i33"] = 60;---//
    //no TV service selected
    //i1
    disc["dv1_tv0_i1"] = 39.91;
    
    disc["dv2_tv0_i1"] = 49.11;
    
    
    
    function calculate(Form){
      var total = 0;
      var dv = getSelectedRadio(Form.dv);
      var pvtv = getSelectedRadio(Form.pvtv);
      var net = getSelectedRadio(Form.net);
      if (dv && pvtv && net){ //all checked
         var d = disc[dv.id + "_" +pvtv.id + "_" + net.id];
         if (typeof d != "undefined"){  //discount exists in the array
            total = d;
         }
         else{ //no discount
            total = Number(dv.value) + Number(pvtv.value) + Number(net.value);
         }
      }
      else {
         alert("Please select one from each item.");
         return;
      }
      alert("$" + total.toDecimals(2));
    }
    
    function getSelectedRadio(oRadGrp){
      var len=oRadGrp.length;
      for (var i=0; i<len; i++){
         if (oRadGrp[i].checked) return oRadGrp[i];
      }
      return null;
    }
    
    //from liorean (http://www.codingforums.com/showthr...8077#post178077)
    Number.prototype.toDecimals=function(n){
        n=(isNaN(n))?
            2:
            n;
        var
          nT=Math.pow(10,n);
      function pad(s){
                s=s||'.';
                return (s.length>n)?
                   s:
                    pad(s+'0');
        }
        return (isNaN(this))?
            this:
            (new String(
                Math.round(this*nT)/nT
            )).replace(/(\.\d*)?$/,pad);
    }
    </script>
    </head>
    <body>
    <form>
    <fieldset>
    <legend>TService </legend>
    <input name="dv" type="radio" id="dv0" value="0" checked />
    None
    <label for="dv0"> </label>
    <br />
    <input type="radio" name="dv" id="dv1" value="12.75" />
    Line
    <label for="dv1"> - $12.75</label>
    <br />
    <input type="radio" name="dv" id="dv2" value="21.95" />
    <label for="dv2">Basic  - $21.95</label>
    <br />
    <input type="radio" name="dv" id="dv3" value="34.95" />
    <label for="dv3">Unlimited  - $34.95<br>
    <input type="radio" name="dv" id="dv4" value="47.50" />
    Unlimited Plus- $47.50</label>
    <br />
    </fieldset>
    <fieldset>
    <legend>Services </legend>
    <input name="pvtv" type="radio" id="tv0" value="0" checked />
    <label for="tv0">None</label><br>
    <input type="radio" name="pvtv" id="tv1" value="15.17" />
    <label for="tv1">Connector - $15.17</label><br>
    <input type="radio" name="pvtv" id="tv2" value="33.79" />
    <label for="tv2">Plus  - $33.79</label><br>
    <input type="radio" name="pvtv" id="tv3" value="39.95" />
    <label for="tv3"> Digital- $39.95</label><br>
    
    </fieldset>
    <fieldset>
    <legend>IServices</legend>
    <input name="net" type="radio" id="i0" value="0" checked />
    <label for="i0">None</label><br>
    <input type="radio" name="net" id="i1" value="31.95" />
    <label for="i1">Modem - $31.95</label><br>
    <input type="radio" name="net" id="i2" value="34.95" />
    <label for="i2">Modem- $34.95</label><br>
    <input type="radio" name="net" id="i3" value="39.95" />
    <label for="i3">Modem- $39.95</label><br>
    </fieldset>
    <input name="" type="button" onclick="calculate(this.form)" value="Calculate" />  
    </form>
    </body>
    </html>
    Thanks in advance for your help

  • #2
    Senior Coder TheShaner's Avatar
    Join Date
    Sep 2005
    Location
    Orlando, FL
    Posts
    1,126
    Thanks
    2
    Thanked 40 Times in 40 Posts
    Add this input area right after you Calculate button:

    Code:
    <input type="text" name="TotalAmount" size="10" value="" />
    Change your Calculate function to the below:

    Code:
    function calculate(Form){
      var total = 0;
      var dv = getSelectedRadio(Form.dv);
      var pvtv = getSelectedRadio(Form.pvtv);
      var net = getSelectedRadio(Form.net);
      if (dv && pvtv && net){ //all checked
         var d = disc[dv.id + "_" +pvtv.id + "_" + net.id];
         if (typeof d != "undefined"){  //discount exists in the array
            total = d;
         }
         else{ //no discount
            total = Number(dv.value) + Number(pvtv.value) + Number(net.value);
         }
         Form.TotalAmount.value = "$" + total.toDecimals(2);
      }
      else {
         alert("Please select one from each item.");
         return;
      }
    }
    The code in red does the trick. You can change the input box to the size you want. I just threw in a number.

    -Shane
    Last edited by TheShaner; 09-20-2005 at 07:51 PM. Reason: Glenngv pointing out my mistakes, ha

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    10,966
    Thanks
    0
    Thanked 236 Times in 233 Posts
    Javascript is case-sensitive. It should be value not Value.

    Code:
    Form.TotalAmount.value = "$" + total.toDecimals(2);


  •  

    Posting Permissions

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