473,396 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to calculate in multiple line items?

I have a javascript code to take value from two text boxes and calculate on triggering the "OnBlur" function and display in the third box.

The code works fine with one line item,If i have more that one line item.i will be able too calculate the value only for the last line item added ,But if i change any of the values in the two boxes from which it takes values.The probablity will not be recalculated.

Could anyone help me to fix this?

//here is the code.

//code for the text boxes.
<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'] . '"';
}
//$go= $_REQUEST['ITEM_IN'][$i]['PERCENTAGE_GO'] ;?> /></td>
<td class="bodyForm"><input type="text" onBlur="probability()" 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'] . '"';
}

//to display the results
<input type="text" id="answer" onBlur="probability()" name="ITEM_IN[<?php echo $x;?>][PROBABILITY]" size="5" value="<?php echo $_REQUEST['ITEM_IN'][$i]['PROBABILITY']?>" <?php
if (!empty($_REQUEST['ITEM_IN'][$i]['PROBABILITY'])) {
echo ' value="' . $_REQUEST['ITEM_IN'][$i]['PROBABILITY'] . '"';
}?>/></td>




//javascript to calculate the probablity.
<SCRIPT>
function probability()
{
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 ansd = document.getElementById("ITEM_IN[<?php echo $x;?>][PROBABILITY]");
ansd.value = Math.round((val1 * val2)/100);
}

</SCRIPT>

Please help.

Thanks
Jun 20 '07 #1
3 2914
acoder
16,027 Expert Mod 8TB
Could you give the HTML source that is generated (not PHP), i.e. what values are generated by the PHP code? Also, give an example of at least two rows, so we can work with that.
Jun 20 '07 #2
Hi ,

Thanks for your replay,Below is the generated HTML code.


HTML FOR FIRST LINE ITEM
tr class="headerBG">

<th class="bodyTextWhite">% Go</th>
<th class="bodyTextWhite">% Win</th>
<th class="bodyTextWhite">Probability</th>



<SCRIPT LANGUAGE="JavaScript">
function probability()
{
var val1 = parseInt(document.getElementById("ITEM_IN[0][PERCENTAGE_GO]").value);
var val2 = parseInt(document.getElementById("ITEM_IN[0][PERCENTAGE_WIN]").value);
var ansd = document.getElementById("ITEM_IN[0][PROBABILITY]");
ansd.value = Math.round((val1 * val2)/100);
}
</SCRIPT>

<tr>

<td class="bodyForm"><input type="text" name="ITEM_IN[0][PERCENTAGE_GO]" maxlength="10" size="3" value="45" /></td>
<td class="bodyForm"><input type="text" onBlur="probability()" name="ITEM_IN[0][PERCENTAGE_WIN]" maxlength="10" size="3" value="78" /></td>
</tr>

//THIS TEXT BOX DISPLAYS CACULATED VALUE FOR FIRST LINE ITEM<td class="bodyForm">

<input type="text" id="answer" readonly = "readonly" onBlur="probability()" name="ITEM_IN[0][PROBABILITY]" size="5" value="35" value="35"/></td>



//SECOND LINE ITEM
<th class="bodyTextWhite">% Go</th>
<th class="bodyTextWhite">% Win</th>
<th class="bodyTextWhite">Probability</th>

<SCRIPT LANGUAGE="JavaScript">
function probability()
{
var val1 = parseInt(document.getElementById("ITEM_IN[1][PERCENTAGE_GO]").value);
var val2 = parseInt(document.getElementById("ITEM_IN[1][PERCENTAGE_WIN]").value);
var ansd = document.getElementById("ITEM_IN[1][PROBABILITY]");
ansd.value = Math.round((val1 * val2)/100);
}
</SCRIPT>

<td class="bodyForm"><input type="text" name="ITEM_IN[1][PERCENTAGE_GO]" maxlength="10" size="3" value="56" /></td>
<td class="bodyForm"><input type="text" onBlur="probability()" name="ITEM_IN[1][PERCENTAGE_WIN]" maxlength="10" size="3" value="89"/></td>

//THIS TEXT BOX DISPLAYS CACULATED VALUE FOR SECOND LINEITEM

<td class="bodyForm">


<input type="text" id="answer" readonly = "readonly" onBlur="probability()" name="ITEM_IN[1][PROBABILITY]" size="5" value="50" value="50"/></td>

//SECOND LINE ITEM

<th class="bodyTextWhite">% Go</th>
<th class="bodyTextWhite">% Win</th>
<th class="bodyTextWhite">Probability</th>
<SCRIPT LANGUAGE="JavaScript">


function probability()
{
var val1 = parseInt(document.getElementById("ITEM_IN[2][PERCENTAGE_GO]").value);
var val2 = parseInt(document.getElementById("ITEM_IN[2][PERCENTAGE_WIN]").value);
var ansd = document.getElementById("ITEM_IN[2][PROBABILITY]");
ansd.value = Math.round((val1 * val2)/100);
}
//THIRD LINE ITEM
</SCRIPT>

<tr>
<td class="bodyForm"><input type="text" name="ITEM_IN[2][PERCENTAGE_GO]" maxlength="10" size="3" /></td>
<td class="bodyForm"><input type="text" onBlur="probability()" name="ITEM_IN[2][PERCENTAGE_WIN]" maxlength="10" size="3" /></td>

<td class="bodyForm">

//THIS TEXT BOX DISPLAYS CACULATED VALUE FOR THIRD LINE ITEM

<input type="text" id="answer" readonly = "readonly" onBlur="probability()" name="ITEM_IN[2][PROBABILITY]" size="5" value="" /></td>




<input type="submit" value="New Task" name="itemBTN" />(Button to add new Line Item)
<input type="submit" value="Submit" name="submitBTN" />(Button to Submit the Line items)

Let me know if you have more clarification.

Thanks.
Jun 21 '07 #3
acoder
16,027 Expert Mod 8TB
You're overwriting probability. Instead have one generic function which takes a parameter (say, index). Then modify as below:
Expand|Select|Wrap|Line Numbers
  1. function probability(index)
  2. {
  3. var val1 = parseInt(document.getElementById("ITEM_IN["+index+"][PERCENTAGE_GO]").value);
  4. var val2 = parseInt(document.getElementById("ITEM_IN["+index+"][PERCENTAGE_WIN]").value);
  5. var ansd = document.getElementById("ITEM_IN["+index+"][PROBABILITY]");
  6. ansd.value = Math.round((val1 * val2)/100);
  7. }
Notice how I have replaced 0, 1, 2, etc. with 'index'. When you call the function, call it with 0,1,2,etc.
Jun 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
1
by: Ladislau S. | last post by:
Dear reader, I am an occasional user of MS Access 2000 running on Windows 98. My hobby is ship model building so I made a database for things that I want to buy. After two strokes I bin unable...
1
by: Aaron Prohaska | last post by:
I'm having the problem with this drop down list on postback. For some reason both the ListItems get selected when I change the selected item. Using the code below I'm building the drop down list in...
3
by: D. Shane Fowlkes | last post by:
Sorry for the length of this post. I have created a rather complex form which has a header/line item (parent and child records) structure. It's for an intranet. A screenshot can be seen here: ...
6
by: James Radke | last post by:
Hello, I have a multithreaded windows NT service application (vb.net 2003) that I am working on (my first one), which reads a message queue and creates multiple threads to perform the processing...
12
by: paii, Ron | last post by:
Sorry about that last one. Does anyone know how to calculate the width a string of text for given Font name and size? I want to buildup a block of text strings to display in a unbound control,...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
1
by: favor08 | last post by:
have serval date fields in my table that i need to perform calcualations to bring bring a certain # of items. Fields: RptDte Date filed closed always the last date of previous month--- 6/30 for...
1
by: whnkee | last post by:
I need to audit the freight bills charged by freight company which has over 20,000 consignment per week. The basic charge is base on distance and weight, something like this: SYD-MEL <1kg $6...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.