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
    Dec 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Please help - simple script that won't work!

    I am a very novice web coder and can't get this to work... I've tried everything and am getting no where but frustrated! Thanks in advance!!

    Code:
    <html>
    
    <head>
    
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function  calculateSavings() 
    {
    	var lastBill = eval(document.theForm.elements[0].value);
    	var frequencyRating = eval(document.theForm.elements[1].value);
    	var savingsRate = .05;
    	var annualSavings = 0;
    	
    /*	
    	if (frequencyRating == 'every day)
    	{
    		savingsRate = .01;
    	}
    	else if (frequencyRating == 'weekly)
    	{
    		savingsRate = .05;
    	}
    	else if (frequencyRating == 'monthly')
    	{
    		savingsRate = .15;
    	}
    	else if (frequencyRating == 'yearly')
    	{
    		savingsRate = .2;
    	}
    */
    	
    	annualSavings = lastBill * savingsRate;
    	
    	document.write(annualSavings);
    }
    // End -->
    </SCRIPT>
    
    </head>
    
    <body>
    
    <form name="savingsCalcForm">
    How much was your last bill?
    <br><INPUT TYPE="text">
    <br>
    <br>
    How often are you using your tractor?
    <br><input type="radio" name="group1" value="every day"> every day<br>
    <input type="radio" name="group1" value="weekly "> weekly<br>
    <input type="radio" name="group1" value="monthly"> monthly<br>
    <input type="radio" name="group1" value="yearly"> yearly<br>
    <br>
    <br>
    <INPUT TYPE="button" VALUE="calculate savings" onClick="calculateSavings();">
    
    </form>
    
    </body>
    
    </html>

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by agdavidson View Post
    I am a very novice web coder and can't get this to work...
    This should do what? Please, tell us what should do that code, as well.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Homework? Have you tried using your error console? There are quite a few errors and you seem unaware of how to capture the value of a checked radio button.

    Code:
    <script type = "text/javascript">
    
    function  calculateSavings() {
    var lastBill = document.savingsCalcForm.elements[0].value;
    var len = document.savingsCalcForm.group1.length;
    for (var i =0; i< len;  i++){
    if (document.savingsCalcForm.group1[i].checked) {
    var frequencyRating = document.savingsCalcForm.group1[i].value;
    }
    }
    ///.....
    Therre are other errors as well in the commented out section. If you had used " instead of ' they would have been more obvious.

    a) Do not use eval!! Insecure and totally pointless here, but in any case mostly undesirable as there is almost always a better method.

    b) Prefer to use the names of the form elements rather than elements[0] - you should assign a name to your textbox.

    c) You need to validate the value of lastBill entered into the textbox to ensure that it is a positive number. Obviously you cannot multiply a string value.

    d) <script language=javascript> is long deprecated and obsolete. Use <script type = "text/javascript"> instead. The <!-- and //--> comment (hiding) tags have not been necessary since IE3 (i.e. since September 1997). If you see these in some published script it is a warning that you are looking at ancient and perhaps unreliable code.

    e) If you made the values of the radio buttons 0.01, 0.05 etc. then you would be able to take these directly as the value of frequencyRating.
    Code:
    annualSavings = lastBill * frequencyRating;
    f) document.write statements must be run before the page finishes loading. You will have noticed that any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the - doubtless unintended - impression that you take other people's voluntary unpaid assistance and expertise for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.



    "In the beginner's mind there are many possibilities, but in the expert's mind there are few” - Shunryu Suzuki (Japanese Zen priest, ?-1971)
    Last edited by Philip M; 12-21-2010 at 12:25 PM. Reason: Noticed typo


  •  

    Posting Permissions

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