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
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question No Response from my Script - Am I missing Something

    Hi There,

    Hope you can help.

    I have a mini form, for which I am trying to calculate a price based on 3 variables. I thought it was correct, but I dont get a response.
    Please let me know if you can help with this?

    JAVASCRIPT:
    Code:
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function getPriceForDays() {
    if (weeks == 0, days == 1) return (price / 0.5);
    if (weeks == 0, days == 2) return (price / 0.7);
    if (weeks == 0, days == 1) return (price / 0.9);
    if (weeks >= 1, days == 3) return (price / 0.2);
    if (weeks >= 1, days == 2) return (price / 0.4);
    if (weeks >= 1, days == 1) return (price / 0.2);
    }
    function getPriceForWeeks() {
    if (weeks >= 0) return (price * weeks)
    }
    function getNumberOfWeeks() {
    var weeks = document.calculator.weeks.value; 
    return (weeks == "") ? 0 : weeks;
    }
    function getNumberOfDays() {
    var days = document.calculator.days.value; 
    return (days == "") ? 0 : days;
    }
    function getPrice() {
    var price = document.calculator.price.value; 
    return (price == "") ? 0 : price;
    }
    function showResult(result) {
    	document.calculator.response.value = result;
    }
    fuction formatPrice(value) {
    var result = (days * weeks);
    return result;
    
    // End -->
    </Script>
    FORM:
    Code:
    <form id="form1" name="calculator">
      <table width="44%" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td width="22%">Days</td>
          <td width="78%"><label>
            <input name="days" type="text" id="days" />
          </label></td>
        </tr>
        <tr>
          <td>Weeks</td>
          <td><input type="text" name="weeks" /></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
          <td><p>
            <input type="hidden" name="price" value="100"/>
          </p>
          <p>
            <label>
            <input type="submit" name="Submit" value="Submit" onClick="formatPrice(value)"/>
            </label>
    </p></td>
        </tr>
        <tr>
          <td>Total Price </td>
          <td><label>
            <input type="text" name="response" />
          </label></td>
        </tr>
      </table>
      <p>&nbsp;</p>
    </form>
    </body>
    Basically, the code is for a rental, based on a number of days and weeks. The price is a hidden field. The problem i have is that the cost per day changes once the value of weeks is greater that 0.

    I am new to this but have a good knowledge of HTML, and a growing knowldge of PHP, and Perl.

    Cheers
    Tim

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,172
    Thanks
    203
    Thanked 2,549 Times in 2,527 Posts
    if (weeks == 0, days == 1) return (price / 0.5);
    if (weeks == 0, days == 2) return (price / 0.7);
    if (weeks == 0, days == 1) return (price / 0.9);

    The syntax is incorrect and weeks/0 days/1 is repeated.

    Try:-

    if (weeks == 0 && days == 1) return (price / 0.5);
    if (weeks == 0 && days == 2) return (price / 0.7);
    if (weeks == 0 && days == 3) return (price / 0.9);

  • #3
    Regular Coder Lerura's Avatar
    Join Date
    Aug 2005
    Location
    Denmark
    Posts
    947
    Thanks
    0
    Thanked 130 Times in 129 Posts
    your javascript is totally messed up

    1.
    a if is constructed this way:
    if (condition){statement}

    2.
    you must use "&&" instead of ","
    if (weeks == 0 && days == 1){}

    3.
    A function cannot run unless you call it.
    you are trying to use then as automatic subrutines.

    Beside that:

    My suggestion is that you skip the weeks so that when you're eg. want a 10 day rental you enter "10 days" instead of "1 weeks and 3 days"

    if i'm right you have 2 different rent's per day. a high for the first 7 days and a
    low for the rest of the days.

    If so, you can use this:

    Code:
    <script><!--
    HighRent=100;
    LowRent=50;
    
    function formatPrice(){
    
    Days = document.calculator.days.value;
    if (Days == ""){Days = 0}
    Days = eval(Days);
    
    if (Days > 7){
    HighDays = 7;
    LowDays = Days - 7;
    }else{
    HighDays = Days;
    LowDays = 0;
    }
    
    TotalRental = HighDays * HighRent + LowDays * LowRent;
    return TotalRental;
    }
    // --></script>
    if you are submitting it the right way i dont know


  •  

    Posting Permissions

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