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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable in a variable

    <SCRIPT>

    function calculate(form)
    {
    totalcount = form.count.value;
    for (i = 1; i < totalcount; i++) {
    itemcost = form.a2.value;
    form.cost.value = itemcost;
    }
    }
    </SCRIPT>

    I am passing data from multiple forms to this script which look like this:
    NAME VALUE
    a1 10.23
    a2 5.34
    a3 2.24


    I need to add up the values and and display using form.cost.value later in the page. The above example simply displays 5.34.

    I believe what I want is itemcost = form.a(value of i here).value, but I am stuck on how to do it. As you can tell I am a javascript newbie.

    Shane

  • #2
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't quite understand, could you post the complete code?
    Shawn

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use a hashtable lookup.
    PHP Code:
    var elem = [I]formName[/I].elements.[I]elementName[/I];
    var 
    value elem.value


    PHP Code:
    function calculate(form) {
        var 
    formElements form.elements;
        var 
    totalCost 0;
        var 
    elms = [formElements.a1formElements.a2formElements.a3];
        for ([
    B]var[/B0elms.lengthi++) {
            var 
    value parseFloat(elms[i].value);
            if(
    isNaN(value)) {
                
    handleNonNumericData(elems[i], value);
                return;
            }
           
    totalCost += value;
        }
         
    form.cost.value totalCost;


    function 
    handleNonNumericData(elemvalue) {

        
    alert(elem.name ": "+value " is not a value");


    Don't forget to use var.

  • #4
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I should have been more specific. I am pulling the fields from a database and there will be different numbers of rows as data is added. That is why it needs to increment using a variable. I have it working in PHP, but I don't want the screen to refresh when the total is calculated. Here is the code:

    PHP Code:
    <?php require_once('Connections/MySQL.php'); ?>
    <?php
    mysql_select_db
    ($database_MySQL$MySQL);
    $query_Recordset1 "SELECT * FROM rates";
    $Recordset1 mysql_query($query_Recordset1$MySQL) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <SCRIPT>

    function calculate(form)
    {
      totalcount = form.count.value;
      totalcost = 0.00;
      itemcost = 0.00;
    for (i = 1; i < totalcount; i++) {
      itemcost = form.a2.value;
      //totalcost = 5;
      form.cost.value = itemcost; //totalcost;
    }

    </SCRIPT>
    </head>

    <body>
    <p>&nbsp;</p>

    <table border="1">
    <tr>
        <td>RateID</td>
        <td>RateDesc</td>
        <td>RatePrice</td>
        <td>Select</td>
    </tr>    
      <form method="post">

      <?php 
      $count 
    0;
      do { 
    ?>
      <tr>
        <td><?php echo $row_Recordset1['RateID']; ?></td>
        <td><?php echo $row_Recordset1['RateDesc']; ?></td>
        <td><?php echo $row_Recordset1['RatePrice']; ?></td>
        <td><input type="checkbox" name="<?php echo "a"$row_Recordset1['RateID']; ?>" value="<?php echo $row_Recordset1['RatePrice']; ?>"></td>
        
      </tr>
      <?php $count++; } while ($row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>
      
      <input type="hidden" name="count" value="<? echo $count?>">
      
      <tr><td><input type=button onsubmit="calculate(this.form)" submit value="Calculate"></td></tr>
      <tr>
    <td valign=top align=right><tt><b>Total: $</b></td>
    <td valign=top><input type="text" name="cost" value="" size="5">
    </td>
    </tr>
      </form>
      </table>
      
      

      
    <p>&nbsp;</p>
    </body>
    </html>
    <?php
    mysql_free_result
    ($Recordset1);
    ?>
    I have made some changes trying to get it to work. Thanks.

    Shane

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This fuction will not do anything meaningful:



    function calculate(form)
    {
    totalcount = form.count.value;
    totalcost = 0.00;
    itemcost = 0.00;
    for (i = 1; i < totalcount; i++) {
    itemcost = form.a2.value;
    //totalcost = 5;
    form.cost.value = itemcost; //totalcost;
    }
    }
    First, think about what you want the function to do. you want it to add the values of some type of input, right? Then name your function something like sumOfInputs()

    Now, look at the first variable:
    var totalcount = form.count.value;

    What is this? Well, lets examine:


    alert(typeof totalcount); // "string"
    alert(totalcount); // depends on the value in the HTML Form Element.
    alert(totalcount.length); // String length of the value in the HTML Form Element.

    Why would you want to loop totalcount.length times?

  • #6
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I realize the function does nothing now, what you see is a lot of leftovers from testing various things. I am sure there is a much better way to do this, that is why I need help.

    Here is my logic, flawed as it may be:

    What the page does is reads a database table of an undefined number of rows (more rows will be added over time) and displays each row with a checkbox next to it. The value of the checkbox becomes the value of RatePrice. The user selects which item(s) to sum and then submits to the function.

    I believe the function receives the form name and value for each row. totalcount holds the number of forms that is passed. The purpose of the loop is to increment the form name and add the values. As I learn more it seems that this is the wrong way (or very long way) to acheive the goal of adding all the input from the form, but again I am a newbie.

    The onsubmit in this line should be onclick I believe:
    <tr><td><input type=button onsubmit="calculate(this.form)" submit value="Calculate"></td></tr>

    Maybe I need to totally scrap what I have and start over

  • #7
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here is the PHP code that calculates properly. I was hoping to use javascript to display the result on the same page without a refresh.

    PHP Code:
    <?php require_once('Connections/MySQL.php'); ?>
    <?php
    mysql_select_db
    ($database_MySQL$MySQL);
    $query_Recordset1 "SELECT * FROM rates";
    $Recordset1 mysql_query($query_Recordset1$MySQL) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    </head>

    <body>
    <p>&nbsp;</p>

    <table border="1">
    <tr>
        <td>RateID</td>
        <td>RateDesc</td>
        <td>RatePrice</td>
        <td>Select</td>
    </tr>    
      <form name="select" method="post" action="calculate.php">

      <?php 
      $count 
    0;
      do { 
    ?>
      <tr>
        <td><?php echo $row_Recordset1['RateID']; ?></td>
        <td><?php echo $row_Recordset1['RateDesc']; ?></td>
        <td><?php echo $row_Recordset1['RatePrice']; ?></td>
        <td><input type="checkbox" name="<?php echo $row_Recordset1['RateID']; ?>" value="<?php echo $row_Recordset1['RatePrice']; ?>"></td>
        
      </tr>
      <?php $count++; } while ($row_Recordset1 mysql_fetch_assoc($Recordset1)); ?>
      
      <input type="hidden" name="count" value="<? echo $count?>"
      <tr><td><input type=submit value="Calculate"></form></td></tr>
      </table>
      
    <p>&nbsp;</p>
    </body>
    </html>
    <?php
    mysql_free_result
    ($Recordset1);
    ?>

    And calculate.php:

    PHP Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <?php 
    $total
    =0.00;
    for (
    $i=1$i <= $HTTP_POST_VARS['count']; $i++)
        {
            
    $temp"$i";
            
    $total $total $HTTP_POST_VARS[$temp];
        }
    echo 
    "Total Price: $"$total
    ?>

    </body>
    </html>

  • #8
    Regular Coder
    Join Date
    Mar 2004
    Posts
    130
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Dunno cause I'm a PHP newb, but I think you need a backslash here:

    PHP Code:
    "Total Price: \$"$total 

    PHP Code:
    for ($i=1$i <= $HTTP_POST_VARS['count']; $i++)
        {
            
    $temp"$i";
            
    $total $total $HTTP_POST_VARS[$temp];
        }
    echo 
    "Total Price: $"$total 
    $HTTP_POST_VARS is a hashtable, or assoc array. In js, you can use an array, Just like I showed you before.
    PHP Code:
    function calculate(form) {
        var 
    formElements form.elements;
        var 
    totalCost 0;
        var 
    elms = [formElements.a1formElements.a2formElements.a3];
        for (var 
    0elms.lengthi++) {
            var 
    value parseFloat(elms[i].value);
            if(
    isNaN(value)) {
                
    handleNonNumericData(elems[i], value);
                return;
            }
           
    totalCost += value;
        }
         
    form.cost.value totalCost;


    function 
    handleNonNumericData(elemvalue) {

        
    alert(elem.name ": "+value " is not a value");

    Think of elms as the same thing as $HTTP_POST_VARS, except instead of an assoc array, it's just an array with numeric indices.


    PHP Code:
     var elms = [formElements.a1formElements.a2formElements.a3]; 
    Think of elms as the same thing as $HTTP_POST_VARS, except instead of an assoc array, it's just an array with numeric indices.

  • #9
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry, but I still don't understand this line:

    var elms = [formElements.a1, formElements.a2, formElements.a3];

    Since there may be 3 forms submitted or 10 or 15 would I need to have a formElements.?? for each form and therefore if rows were added to the database I would need to edit this line? I am probably missing something. You should be able to see what I am working on here . Thanks for your help so far.

    SB

  • #10
    Regular Coder
    Join Date
    Feb 2004
    Location
    WA
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This function should sum the checkboxes up as long as the rate id's look like a1, a2, a3, etc...

    Code:
    function calculate(form) 
    { 
        var formElements = form.elements; 
        var totalCost = 0; 
        for (var i = 0; i < form.count.value; i++) 
        { 
            var frmElement = formElements["a"+i];
            if (frmElement.checked == true)
            {
                totalCost +=  parseFloat(frmElement.value); 
            } 
        }
        form.cost.value = totalCost; 
    }
    Nobody is Perfect. I am Nobody.

  • #11
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the response, I tried the code and I get an error when it calculates.

    See here

  • #12
    New to the CF scene
    Join Date
    Mar 2004
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Never mind the last post. I changed the line:
    for (var i = 0; i < form.count.value; i++)

    to

    for (var i = 1; i <= form.count.value; i++)

    and it seems to be working properly. Thanks!!


  •  

    Posting Permissions

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