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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Passing values to a function

    I am trying to create a function that will calculate the cost of an item based on a quantity entered and the price per item. I want to reuse this function multiple times because there will be multiple items on the final form. Here is my code:

    Code:
    <html>
    <head>
    <title></title>
    
    <script language="Javascript">
    <!--
    function calcTotal()
    {
    	var prod_price = 38.75;
    	var prod_total = prod_price * document.order.prod1_qty.value;
    	document.order.prod1_sub.value = prod_total;
    }
    //-->
    </script>
    </head>
    
    <body>
    
    <form name="order">
    <p>Item#01</p>
    <p>Price: $38.75</p>
    
    <p>Quantity: 
    <input type="text" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal();"></p>
    
    <p>Total: $
    <input type="text" name="prod1_sub" size=7></p>
    </form>
    
    </body>
    </html>
    Obviously this code works, but I am not sure how to manipulate the code to pass the necessary information to the function so it will work for multiple calculations.

    Any help is greatly appreciated.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    try it like this
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title></title>
    
    <script language="Javascript">
    <!--
    function calcTotal(what,which1,which2)
    {
    	var prod_price = what;
    	var prod_total = prod_price * document.getElementById(''+which1+'').value;
    	document.getElementById(''+which2+'').value = prod_total;
    }
    //-->
    </script>
    </head>
    
    <body>
    
    <form name="order">
    <p>Item#01</p>
    <p>Price: $38.75</p>
    
    <p>Quantity: 
    <input type="text" id="prod1_qty" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('38.75','prod1_qty','prod1_sub');"></p>
    
    <p>Total: $
    <input type="text" id="prod1_sub" name="prod1_sub" size=7></p>
    <p>Item#02</p>
    <p>Price: $99.99</p>
    
    <p>Quantity: 
    <input type="text" id="prod2_qty" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('99.99','prod2_qty','prod2_sub');"></p>
    
    <p>Total: $
    <input type="text" id="prod2_sub" name="prod1_sub" size=7></p>
    </form>
    
    </body>
    </html>

  • #3
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,516
    Thanks
    3
    Thanked 505 Times in 492 Posts
    PHP Code:
    <html>
    <
    head>
    <
    title></title>

    <
    script language="Javascript">
    <!--
     var 
    prod_price,prod_total;

    function 
    calcTotal(from,to){
     if (
    isNaN(document.order[from].value)){
      
    alert('Is Not a Number');
      return;
     }
     
    prod_price 38.75;
     
    prod_total prod_price document.order[from].value;
     
    document.order[to].value prod_total;
    }
    //-->
    </script>
    </head>

    <body>

    <form name="order">
    <p>Item#01</p>
    <p>Price: $38.75</p>

    <p>Quantity:
    <input type="text" name="prod1_qty" size=3 maxlength=2 onkeyup="calcTotal('prod1_qty','prod1_sub');"></p>
    <p>
    <input type="text" name="prod2_qty" size=3 maxlength=2 onkeyup="calcTotal('prod2_qty','prod1_sub');"></p>

    <p>Total: $
    <input type="text" name="prod1_sub" size=7></p>
    </form>

    </body>
    </html> 

  • #4
    Regular Coder
    Join Date
    Jan 2005
    Posts
    140
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Thanks for the replys. Unfortunately I can use the PHP code because the my client is not paying for PHP as part of their hosting package, if they were this would make things much easier.

    As far as the code from _Aerospace_Eng_, it works great! My only concern is that I know getElementById does NOT work in older Netscape browser versions.

    Any other thoughts?

    Are there other ways to make reference to the names of the inout fields?

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    you could probably modify it to use document.layers which i believe does work for the older browsers, but i wouldn't worry about it, NS4 is "dead", and vwphillips solution isn't php, its javascript, he just used the php vb code for the syntax coloring, so his way may work also with a slight modification of the price_value
    Last edited by _Aerospace_Eng_; 03-16-2005 at 09:09 PM.

  • #6
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    vwphillips's example is javascript, maybe you should try it out


  •  

    Posting Permissions

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