Forum Moderators: open

Message Too Old, No Replies

decreases total when select

         

nanat

10:42 am on Dec 3, 2009 (gmt 0)

10+ Year Member



Sory Guys For Being annoying i :(..
javascript:

function GetAmount(amount, many)
{
var SboAmount = 200000;
var amount;

}


php code: tree loops in a row


value = 3;
for($r=0; $r<$value; $r++)
{
<select name="Actual[]" id="Actual" onChange="GetAmount('this.value', 'mirkado<?php echo $r; ?>')">
<option value=""></option>
<option value="30">Thirty Percent</option>
<option value="50">Fifty Percent</option>
<option value="70">Seventy Percent</option>
<option value="90">Ninety Percent</option>
<option value="100">One hundred Percent</option>
</select>
}
<input type="text" id="rmaing" name="rmaing" value="" readonly="readonly"/>

my question is how can i get the value base on my loops $r? then pass it to my javascript GetAmount

the difficult part assuming you selected
1st loop var SboAmount - 1stSelected = value
2nd loop value - secondSelected = 1stvalue
3rd loop 1stvalue - 3rdSelected = value

result : document.getElementById('rmaing').value = 3rd loop;

Fotiman

4:11 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm afraid I don't understand what you are asking. But here's a few suggestions:

1. onChange="GetAmount('this.value', 'mirkado<?php echo $r; ?>')"
I don't think you want those quotes there, else it will pass the string value "this.value" to GetAmount. I think you're trying to pass the value of the select element, so remove those quotes:
onChange="GetAmount(this.value, 'mirkado<?php echo $r; ?>')"

2. The second value that you're passing to GetAmount is also going to be a string like "mirkado0", "mirkado1", "mirkado2". If you want this as a numeric value, then why not just pass the number as a separate parameter?


function GetAmount(amount, x, r) {
// ...
}

Then:
onChange="GetAmount(this.value, 'mirkado', <?php echo $r; ?>)"
Now in your GetAmount function, variable r is going to be 0 or 1 or 2.

Unfortunately, I don't understand what you mean by 1stSelected vs. 1stValue.

rocknbil

8:31 pm on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sory Guys For Being annoying

Not annoying, keeps our problem solving fresh. :-)

my question is how can i get the value base on my loops $r? then pass it to my javascript GetAmount

So you have a series of select lists, so if $r = 3, you have three, right?

You are using PHP arrays for the object names, and while there is probably a way to do this, I prefer to actually name my selects for clarity. Example shown below. Before the example, note this:

function GetAmount(amount, many)
{
var SboAmount = 200000;
var amount;
}

You have already defined the variable amount as an input parameter.

Last, you access select lists by the selected index. The option value, and option text, is accessed by that index, like

var object = [the select list object]
var ind = obj.selectedIndex;
var value = obj.options[ind].value;

But in looking at this . . . doesn't even look like you need to pass a reference of the current object. You want to tally up all three, sooooo . . . . . tested and working, should get you started.


<?php
header("content-type:text/html");
// returning false so my test doesn\'t submit
// also storing everything in $out so it prints
// to the screen all at once
$out = '
<p>TEST</p>
<form action="" onSubmit="return false;">
';
$rows = 3;
$options = Array (
'30' => 'Thirty',
'50' => 'Fifty',
'70' => 'Seventy',
'90' => 'Ninety',
'100' => 'One Hundred'
);
$js = Array ();
for($r=0; $r<$rows; $r++) {
// when your script receives these, just explode on "_"
$objName = 'Actual_' . $r;
// Watch what we do with this in the JS
array_push($js,"'$objName'"); // Including the quotes
$out .= '
<select name="' . $objName . '" id="' . $objName . '" onChange="GetAmount()">
<option value="0">SELECT</option>';
foreach ($options as $key=>$value) {
$out .= '<option value="' . $key . '">' . $value . ' Percent</option>';
}
$out .= '
</select>
';
}
// Note I\'m using PHP to build the JS array to match $rows.
$out .=
'<input type="text" id="rmaing" name="rmaing" value="" readonly="readonly"/>
</form>
<script type="text/javascript">
// Sorry, don\'t know what many is
function GetAmount() {
var SboAmount = 200000;
var ind,val,thisObj;
var objects = new Array(' . join(',', $js) . ');' . // or implode
'for(r=0; r<' . $rows . '; r++) {
thisObj = objects[r];
ind = document.getElementById(thisObj).selectedIndex;
val = document.getElementById(thisObj).options[ind].value;
SboAmount = parseInt(SboAmount-val); // or divide, multiply, whatever
}
document.getElementById(\'rmaing\').value=SboAmount;
alert(\'Final value is \' + document.getElementById(\'rmaing\').value);
}
</script>
';
echo $out;
?>

nanat

4:00 am on Dec 4, 2009 (gmt 0)

10+ Year Member



wow. great rocknbil but when i changes the SboAmount = SboAmount * val;
the behavior is so wierd

assuming SboAmount = 1000
1st = 30;
2nd = 30;
3rd = 30;

the 1st loop return 0, 2nd return 0, 3rd return = 27000000 the sum of all total.. when i put the \'rmaing\' inside the forloops ofcorz it will return 3 messages box, the 1st box of each loops gives true data but the 2 and 3 return 0..

another question:
correct me if wrong the logic of making parentage is c =(A * b)/100;


1st = 30;
2nd = 30;
3rd = 30;

SboAmount = (SboAmount) * (val) /100;
document.getElementById(\'rmaing\').value=SboAmount;


Return false

whats wrong with my logic :(

nanat

5:15 am on Dec 4, 2009 (gmt 0)

10+ Year Member



so the result would be
value1 = 1st loop 0.30 * SboAmount = 300 - SboAmount = 700
value2 = 2nd loop 0.30 * value1 = 210 - value1 = 490
value3 = 3rd loop 0.30 * value2 = 343 - value2 = 343

value1 = 700
value2 = 490
value3 = 343

remaining balance = 343.. :( hahaha..

rocknbil

7:57 pm on Dec 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, what you are saying is the amount decreases the total by the selected percentage for each of the three, right? if 30 is selected first, decrease total by 30%, etc.

It should be basic math . . . . you should only need to change the one link, and add a second one to insure monetary format. I did a precursory test on the code above, changing it only like so.

SboAmount = SboAmount-(SboAmount*(val/100));
// To give it the format 123.00
SboAmount = SboAmount.toFixed(2);

nanat

12:37 am on Dec 7, 2009 (gmt 0)

10+ Year Member



amazing rocknbil. I'm really weak in math operation perfect. tnx..^^