cheechm
09-06-2008, 02:05 PM
Hi,
So I have two text fields:
Each and Total
Each is the cost of the items sereratley, and total is the cost of that item times the quanitity.
How would I be able to update the field total, when a value in each is changed?
Like Total = value of each * quantity (which is taken from php: $number['total']
Thanks
rangana
09-06-2008, 02:17 PM
Hope this helps:
<script type="text/javascript">
window.onload=function()
{
document.getElementById('each').onkeyup=function()
{
var total=Number(document.getElementById('quantity').value)*Number(this.value),
sTotal=document.getElementById('total');
sTotal.value=(!isNaN(total))?total:'That is not a number';
}
}
</script>
<label for="quantity">Quantity: </label><input type="text" id="quantity" value="5"><br>
<label for="each">Each: </label><input type="text" id="each"><br>
<label for="total">Total: </label><input type="text" id="total" readonly="readonly">
cheechm
09-06-2008, 02:54 PM
Hi,
that wouldn't work because this is what it looks like:
<th>Item</th><th>Each</th><th>Cost</th>
</tr>
<tr>
<td><b>1x</b> I-Rock 5S </td>
<td>£<input type="input" class="text" id="eachI-Rock 5S" name="eachI-Rock 5S" value="100" size="4" maxlength="5" onblur="name.value=='this.value * 1" /></td>
<td>£<input type="text" name="I-Rock 5S" id="I-Rock 5S" class="text" size="4" maxlength="5" value="100" /></td>
</tr>
<tr>
<td><b>1x</b> Biglite </td>
<td>£<input type="input" class="text" id="eachBiglite" name="eachBiglite" value="254" size="4" maxlength="5" onblur="name.value=='this.value * 1" /></td>
<td>£<input type="text" name="Biglite" id="Biglite" class="text" size="4" maxlength="5" value="254" /></td>
</tr>
</table>
If you look, "each" also has the name suffixed on, so in PHP it would be "each" . $number['name'] and total is just $number['name']
The quantity comes from $number['total'].
Thanks
rangana
09-07-2008, 05:10 AM
I see difficulty. I can't see any pattern. The name of the textbox of each's and cost's does'nt match.
cheechm
09-07-2008, 01:09 PM
I see difficulty. I can't see any pattern. The name of the textbox of each's and cost's does'nt match.
However can you get the value from PHP? <? echo $number['name'] ?> etc?
rangana
09-07-2008, 03:53 PM
Yes, we can, but not directly unto JS. You can however place the value in a hidden file:
<input type="hidden" value="<?php echo $number['name']; ?>" id="myName">
...and call show the value of that field:
<script type="text/javascript">
window.onload=function(){
alert(document.getElementById('myName').value);
Hope that makes sense.