PDA

View Full Version : calculate me onChange


EvilBernd
10-17-2002, 09:07 PM
Hi there

I just wanted to build a little shop based on PHP and JS, but i got totally messed up with the JS part.

I got multiple drop-downs, which should onChange display there value in a text-field. That wasn't that hard, because I only had to use this little thing:
onChange="document.form.ddownX.value=document.form.textfdX.value" in my <a>-tag. The x is a variable which is determined by PHP.

But now I wanted to create another textfield to show the total-sum of all the other text-fields and changes the textfields if the dropdown-menu is changed, but I don't get it. I tried to use a function called calc(x) which looks like this (by now):

function calc(x){
var ddown=ddown.x
var textfd=textfd.x
document.form.ddown.value=document.form.textfd.value; //change the textfield
}

As you can see its just the code to change the textfields, but even this doesn't work.

So what it wrong in there and how is it possible to calculate the sum and display it in an extra textfield??

Thx a lot EB

beetle
10-17-2002, 09:22 PM
yes, this is all possible, but I'd like to see a bit more code. Got a link online? If not, post some more of it here...

EvilBernd
10-18-2002, 12:58 AM
Hi

Ok this is the important part of this thing - its written in PHP but I think thats not a big problem:

<?
for ($k=0; $k < $num; $k++) {
$row = mysql_fetch_array($dosel);
$id = $row["article_id"];
$many =$row["quant"];
$ename = $row["article"];
$price1 = $row["price"];
for ($r=2;$r<$many+1;$r++){
${'price'.$r}=$price1*$r;
}
echo"
<tr>
<td width='153'><div align='center'><input type='hidden' name='id".$k."' value='".$id."'><input type='hidden' name='ename".$k."' value='".urlencode($ename)."'><font size='2' face='Arial, Helvetica, sans-serif'><a href='javascript:card($id)'>".$ename."</font></a></div>
</td>
<td width='79'>
<div align='center'>
<select name='quant".$k."' onchange='calc($k)'>
<option value='".$price1."' selected>1</option>";
for ($i=2;$i<$many+1;$i++){
echo "<option value='".sprintf("%01.2f", ${'price'.$i})."'>$i</option>";
}
echo"</select></div></td><td>
<div align='center'><input type='hidden' name='price".$k."' value='".$price1."'><font size='2' face='Arial, Helvetica, sans-serif'>".$price1."€</font></div>
</td>
<td width='84'>
<div align='center'>
<input type='text' name='erg".$k."' readonly='readonly' size='5' value='".$price1."'>
</div>
</td>
<td width='57'>
<div align='center'><a href='".$PHP_SELF."?page=shop&delid=".$id."'><font size='2' face='Arial, Helvetica, sans-serif'>delete</font></a></div>
</td>
</tr>";
}
?>

And this is the javascript:
function calc(x){
var erg=erg.x
var quant=quant.x
document.rechne.erg.value=document.rechne.quant.value;
}

glenngv
10-18-2002, 03:05 AM
add additional parameter in calc() function

<select name='quant".$k."' onchange='calc(this,$k)'>


function calc(objSelect,x){
objSelect.form.elements["erg"+x].value=this.options[this.selectedIndex].value;
}