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 2 of 2

Thread: Help Needed.

  1. #1
    New Coder
    Join Date
    Nov 2005
    Posts
    93
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Help Needed.

    Hi

    I am trying to add multiple line items.
    The line items contains two numeric fields a calculate "button" and a field that displays results after calculation .the input for the calculation is taken from the two numeric fields before the button.

    There can be multiple line items like this.
    I wanted help on javascript code to calculate and display the results on each line item.

    here is my code.
    //php code for display of the three field and button.//first numeric field
    <td class="bodyForm"><input type="text" name="ITEM_IN[<?php echo $x;?>][PERCENTAGE_GO]" maxlength="10" size="3"<?php
    if (!empty($_REQUEST['ITEM_IN'][$i]['PERCENTAGE_GO'])) {
    echo ' value="' . $_REQUEST['ITEM_IN'][$i]['PERCENTAGE_GO'] . '"';
    }
    </td>
    //second numeric field
    <td class="bodyForm"><input type="text" name="ITEM_IN[<?php echo $x;?>][PERCENTAGE_WIN]" maxlength="10" size="3"<?php
    if (!empty($_REQUEST['ITEM_IN'][$i]['PERCENTAGE_WIN'])) {
    echo ' value="' . $_REQUEST['ITEM_IN'][$i]['PERCENTAGE_WIN'] . '"';
    }</td>

    //caculate button
    <INPUT TYPE="button" VALUE="PROBABILITY" name="ITEM_IN[<?php echo $x;?>][PROBABLITY]" onclick="javascriptrobablity()" style="font-size: 6pt"></th>

    //to display results of the calclation for each line item.
    <input type="text" id="ITEM_IN[<?php echo $x;?>][PROBABILITY]" name="ITEM_IN[<?php echo $x;?>][PROBABILITY]" size="5" readonly="readonly" value="<?php $_REQUEST['ITEM_IN'][$i]['PROBABILITY'];?>"</td>

    //javascript function to calculate

    <script>
    function probablity()
    {

    var val1 = parseInt(document.getElementById("ITEM_IN[<?php echo $x;?>][PERCENTAGE_GO]").value);
    var val2 = parseInt(document.getElementById("ITEM_IN[<?php echo $x;?>][PERCENTAGE_WIN]").value);
    var ITEM_IN[<?php echo $x;?>][PROBABILITY]= document.getElementById("ITEM_IN[<?php echo $x;?>][PROBABILITY]");
    ITEM_IN[<?php echo $x;?>][PROBABILITY].value = Math.round((val1 * val2)/100);

    }
    </script>

    I am stuck up with this issue.Also not sure if my javascript code is right.
    I am not able to display the results for each of the line item

    Please help me.

    Thanks.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Code:
    //caculate button
    <INPUT TYPE="button" VALUE="PROBABILITY" onclick="probability(this.form, <?php echo $x;?>)" style="font-size: 6pt" />
    
    //to display results of the calclation for each line item.
    <input type="text" id="ITEM_IN[<?php echo $x;?>][PROBABILITY]" name="ITEM_IN[<?php echo $x;?>][PROBABILITY]" size="5" readonly="readonly" value="<?php $_REQUEST['ITEM_IN'][$i]['PROBABILITY'];?>"></td>
    Code:
    function probablity(frm, x)
    {
      var val1 = parseInt(frm.elements["ITEM_IN["+x+"][PERCENTAGE_GO]"].value);
      var val2 = parseInt(frm.elements["ITEM_IN["+x+"][PERCENTAGE_WIN]"].value);
      frm.elements["ITEM_IN["+x+"][PROBABILITY]"].value = Math.round((val1 * val2)/100);
    }
    I assume that the php variable $x is a number. If it is a string, enclose it in single quotes in the button onclick handler.
    Code:
    <INPUT TYPE="button" VALUE="PROBABILITY" onclick="probability(this.form, '<?php echo $x;?>')" style="font-size: 6pt" />
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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