SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    forms, adding items one at a time, how?

    here are my functions for specials on menu items. i need to be able to add a s, m, or l one at a time and produce a total. i dont know how to do this. help.

    btw..am i supposed to post somewhere else or something? my questions never get answered.

    thank so much, great forum.





    //////////////////////////////////////////////////
    // CALCULATION FUNCTION DEFINITION
    /////////////////////////////////////////////////

    /* calTotal() -- function to determine the rate
    * for renting a piece of equipment
    */
    function calTotal() {

    var cost;
    var size; // used for display
    var special; // used for display
    var goodData = true; // used to keep track if input is valid or not
    var display = ""; // string to gather my display text
    var mp;
    var sp;
    var lp;
    var bill;
    var totals = new Array();
    var total="Total";

    // INPUT though the itemID is a digit, we won't be doing any math with
    // it, so leave it as a string
    var type = document.rentals.itemID.value;
    var howLong = document.rentals.time.value;


    // regardless of whether the user entered lower or upper case, I'll change
    // that value to upper case so that we only have to test upper-case letters
    type= itemSiz.toUpperCase();

    // Let's make sure the user entered valid data
    if (type != "s" && type != "m" && type !="l") {
    alert("Invalid item number. Please try again");
    goodData = false;
    }


    // only continue if we have good data
    if (goodData) {

    /* PROCESS: Pizza cost, super order menu viola

    */

    if (type == "s"){
    var small = 5.99;
    }

    else if (mp>1){
    var medTotal=7.99*1
    medtotal+=(4.99*(mp-1));
    }

    else if(lp>3){
    bill=bill-(.10*bill)
    }
    }

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,426
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    post replies

    Quote Originally Posted by base1001
    my questions never get answered.
    Don't be overly sensitive. I wouldn't consider 1 post 6 days ago as being questions, or never (more like my question hasn't been answered yet.)
    Part of the problem may be forum members' reluctance to do your homework assignments for you. But as you posted your code and seem to want to learn I don't think that's the main problem. Most forum members are glad to help solve problems and give advice.
    Part of the problem may be due to a common "newbie" mistake. If you edit your post and put the code inside the bbcode tags [CODE][/CODE], [HTML][/HTML], [PHP][/PHP],
    (by either typing the bbcode tags or highlighting the code and clicking the icon above the post text area), it will make it much easier for others to read and they may be more willing to help.
    As to this post, I don't know what you mean by "1 at a time". If you do
    Code:
    var total = s + m + l;
    this would be the same as
    Code:
    var subtotal = s + m;
    var total = subtotal + l;
    except that you could access the subtotal value if you needed to. Is that what you mean?

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the help. i put 5 hours into it because i am beginner or noob , anyway.

    here is my update. my button does not work. i am stuck again. i started over, and am stuck once again.

    any suggestions?




    <html>
    <head><title>Maria's Pizza Parlor</title></head>
    <body>

    <!-- Display pizza stuff -->
    <h1 align =" center">Pizza Stand</h1>
    <table border =" 2" width =" 30%" align =" center">

    <caption>pizza menu</caption>
    <tr>
    <th>size</th><th>price</th> </tr>
    <tr>
    <td>small</td><td>$5.99 per pizza</td>

    </tr>
    <tr>
    <td>medium</td><td>Buy one for $7.99; get the second for $4.99</td>
    </tr>
    <tr>
    <td>large</td><td>$9.99 per pizza. Buy 3 or more and get 10% off your bill!</td>
    </tr>

    </table><br>
    <hr><hr>

    <!-- create a form for input/output, but organize it with a table -->
    <form name ="pizza"> <table border =" 0" width =" 75%" align =" center">
    <tr>
    <td align="left"> size of pizza<br>
    <input type="text" name="itemID" size="2"></td>

    <td align="right"> number of pizzas<br>
    <input type="text" name="qty" size="2"></td>
    </tr>

    <tr>
    <td colspan="2">
    <input type="button" name="pizzaCost" value="total" onClick="findCost()">
    </td>
    </tr>

    <tr>
    <td colspan="2">
    <input type="textArea" name="output">
    </td>
    </tr>
    </table>
    </form>


    <script language ="javascript">

    //////////////////////////////////////////////////
    // CALCULATION FUNCTION DEFINITION
    /////////////////////////////////////////////////

    var small = 5.99;
    var med = 7.99;
    var medCheap = 4.99;
    var large = 9.99;
    var size;
    var number;
    var total;
    var goodData=false;
    var qty;


    function getData(){
    size=document.pizza.itemID.value;
    size = size.toLowerCase();
    number=parseInt(document.pizza.qty.value);
    if (size != "s" && size != "m" && size != "l")
    {
    alert("try again");
    }
    else if(isNaN(number) || number < 1)
    {
    alert("try again");
    }
    else{
    goodData = true;
    }
    }


    function findCost(){
    getData();

    if(goodData){

    if (size == "s"){
    total = (number * small.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }

    else if(size == "m"){
    total = (number * med.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }

    else if (size == "l"){
    total = (number * large.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }
    }
    }
    //end pizzaCost function



    //////////////////////////////////////////////////
    // OUTPUT
    //////////////////////////////////////////////////

    document.pizza.output.value = "The cost " + total + total.toFixed(2);




    </script>
    </body>
    </html>

  4. #4
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    except that you could access the subtotal value if you needed to. Is that what you mean?
    i don't know, i signed up for the most entry level java class to make sure i wouldn't miss anything, its all so confusing, i am learning though.

    yes, i do need to display the total.

    if you look at the page displayed by the code vice versa, the specials are now gone, i have no errors on the java console(firefox), however i am stuck on just gettting the function to "function".

    thanks again for the reply

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,426
    Mentioned
    160 Post(s)
    Tagged
    1 Thread(s)

    not defined or null

    When a page loads any javascript variables are loaded into memory and and statements not in functions are "run". ie. in this code (again, please use the bbcode tags)
    HTML Code:
    <script language ="javascript">
    <!--
    //////////////////////////////////////////////////
    // CALCULATION FUNCTION DEFINITION
    /////////////////////////////////////////////////
    
    var small = 5.99;
    var med = 7.99;
    var medCheap = 4.99;
    var large = 9.99;
    var size;
    var number;
    var total;
    var goodData=false;
    var qty;
    
    
    function getData(){
    size=document.pizza.itemID.value;
    size = size.toLowerCase();
    number=parseInt(document.pizza.qty.value);
    if (size != "s" && size != "m" && size != "l")
    {
    alert("try again");
    }
    else if(isNaN(number) || number < 1)
    {
    alert("try again");
    }
    else{
    goodData = true;
    }
    }
    
    
    function findCost(){
    getData();
    
    if(goodData){
    
    if (size == "s"){
    total = (number * small.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }
    
    else if(size == "m"){
    total = (number * med.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }
    
    else if (size == "l"){
    total = (number * large.toFixed(2));
    document.pizza.itemID.output="the cost is" + total;
    }
    }
    }
    //end pizzaCost function
    
    //////////////////////////////////////////////////
    // OUTPUT
    //////////////////////////////////////////////////
    
    document.pizza.output.value = "The cost " + total + total.toFixed(2);
    //-->
    </script>
    These vars are "set"
    Code:
    var small = 5.99;
    var med = 7.99;
    var medCheap = 4.99;
    var large = 9.99;
    var size;
    var number;
    var total;
    var goodData=false;
    var qty;
    and because this line isn't in a function, it"s "run"
    Code:
    document.pizza.output.value = "The cost " + total + total.toFixed(2);
    The problem is that when the page loads the variable "total" does not have a value. If you put this line inside the findCost function the browser should stop complaining because it won't look for it's value until it actually has one. (but you still need to make a small change to keep the price from showing twice).


Bookmarks

Posting Permissions

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