Forum Moderators: open
function GetAmount(amount, many)
{
var SboAmount = 200000;
var amount;}
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;
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) {
// ...
}
Unfortunately, I don't understand what you mean by 1stSelected vs. 1stValue.
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;
?>
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;
whats wrong with my logic :(
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);