CodeCharge Studio
search Register Login  

Web Reporting

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 auto calculate 2 text box

Print topic Send  topic

Author Message
inoycavalera

Posts: 6
Posted: 12/11/2011, 6:29 AM

Hi all,

i just use ccs for a week and i'm a newbie in programming that's why i choose ccs. i need help how can i auto calculate 2 text box value and store the result in another text box.

text box 3 = text box 1 * text box 2

how can i do that in ccs code? i found a javascript code to do auto calculate but i don't kno ho to apply it in ccs.

<script type="text/javascript" language="javascript">

function autocalc(oText)
{
if (isNaN(oText.value)) //filter input
{
alert('Numbers only!');
oText.value = '';
}
var field, val, oForm = oText.form, total = a = 0;
for (a; a < arguments.length; ++a) //loop through text elements
{
field = arguments[a];
val = parseFloat(field.value); //get value
if (!isNaN(val)) //number?
{
total += val; //accumulate
}
}
oForm.total.value = total; //out
}


</script>
</head>
<body onload="document.forms[0].reset()">
<form style="width:260px;margin:100px auto;border:2px black dashed;">
<table cellspacing="8">
<tr>
<!-- pass field reference ('this') and other field references -->
<td>value 1___<input name="t1" type="text" onkeyup="return autocalc(this,t2,t3)" tabindex="1"></td>
<td rowspan="3"><strong>total_____</strong><input name="total" type="text" readonly="readonly" value="0" tabindex="-1"></td>
</tr><tr>
<td>value 2___<input name="t2" type="text" onkeyup="return autocalc(this,t1,t3)" tabindex="2"></td>
</tr><tr>
<td>value 3___<input name="t3" type="text" onkeyup="return autocalc(this,t1,t2)" tabindex="3"></td>
</tr>
</table>
</form>
View profile  Send private message
ckroon

Posts: 869
Posted: 12/11/2011, 8:28 AM

I assume it is on a record form...
so you put this code on the On Validate Event.

$tb1 = $Component->TextBox1->GetValue();
$tb2 = $Component->TextBox2->GetValue();

$calculation = ($tb1 + $tb2);

$Component->TextBox3->SetValue($calculation);

YOu can replace TextBox1, 2 and 3 with the names of your textboxes of course..in my example.. TextBox3 would be a Hidden field.
_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
inoycavalera

Posts: 6
Posted: 12/11/2011, 6:24 PM

it works perfectly...thank you very much ckroon
View profile  Send private message
ckroon

Posts: 869
Posted: 12/12/2011, 10:54 PM

Any time!
_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
inoycavalera

Posts: 6
Posted: 12/13/2011, 4:36 AM

Hi ckroon,

can i use your method to make a calculation in a grid? i mean not in a record grid. i need to divide 2 calculation results and divide with 100.

from your example
//calculation 1
$tb1 = $Component->TextBox1->GetValue();
$tb2 = $Component->TextBox2->GetValue();

$calculation = ($tb1 + $tb2);

$Component->TextBox3->SetValue($calculation);

//calculation 2
$tb3 = $Component->TextBox3->GetValue();
$tb4 = $Component->TextBox4->GetValue();

$calculation2 = ($tb3 + $tb4);

$Component->TextBox5->SetValue($calculation2);

i try this in the record grid but give an error..
$test1=$component->textbox3->GetValue();
$test2=$component->textbox5->GetValue();
$calc=($test1/$test2)*100;
$component->result_calc->SetValue($calc);

is it possible to use the method in a grid view?

Thank in advance
View profile  Send private message
inoycavalera

Posts: 6
Posted: 12/13/2011, 5:08 AM

i try this too but the same error happen

$b_qty = $Component->rab_qty->GetValue();
$b_price = $Component->rab_price->GetValue();
$r_qty = $Component->real_qty->GetValue();
$r_price = $Component->real_price->GetValue();
$dif_percent= (($r_qty * $r_price) / ($b_qty * $b_price))*100;
$component->dif_percent->SetValue($dif_percent);

Fatal error: Call to a member function SetValue() on a non-object in E:\xampp\htdocs\ccs\partdetails_maint_events.php
View profile  Send private message
ckroon

Posts: 869
Posted: 12/14/2011, 9:34 AM

check to make sure that

$component->dif_percent->SetValue($dif_percent);

is pointing to your hidden field.. and that it is named properly.

_________________
Walter Kempees...you are dearly missed.
View profile  Send private message
benjohn


Posts: 30
Posted: 12/18/2011, 8:27 AM

hi inoycavalera,

Good Day!

not sure if you mentioned anything about the event in which you placed your codes, try putting your codes in the beforeshowrow event of the grid. hope that helps... all the best!

Pal,
Benjohn
_________________
~=~=~=~=~=~=~=~=~=~=~=
get it done. make it happen.
~=~=~=~=~=~=~=~=~=~=~=
View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

Web Database

Join thousands of Web developers who build Web applications with minimal coding.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.