Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jan 2007
    Posts
    123
    Thanks
    20
    Thanked 1 Time in 1 Post

    Updating text field

    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

  • #2
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Hope this helps:
    Code:
    <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">
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • Users who have thanked rangana for this post:

    cheechm (09-06-2008)

  • #3
    Regular Coder
    Join Date
    Jan 2007
    Posts
    123
    Thanks
    20
    Thanked 1 Time in 1 Post
    Hi,
    that wouldn't work because this is what it looks like:
    Code:
    	<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
    PHP Code:
    "each" $number['name'
    and total is just
    PHP Code:
    $number['name'
    The quantity comes from
    PHP Code:
    $number['total'
    .

    Thanks

  • #4
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    I see difficulty. I can't see any pattern. The name of the textbox of each's and cost's does'nt match.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    123
    Thanks
    20
    Thanked 1 Time in 1 Post
    Quote Originally Posted by rangana View Post
    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?

  • #6
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Yes, we can, but not directly unto JS. You can however place the value in a hidden file:
    PHP Code:
    <input type="hidden" value="<?php echo $number['name']; ?>" id="myName">
    ...and call show the value of that field:
    Code:
    <script type="text/javascript">
    window.onload=function(){
    alert(document.getElementById('myName').value);
    Hope that makes sense.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •