SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow Totaling Form Fields In Javascript..

    I have a script to total the fields of a form, and it works great...but I am having trouble trying to figure out how to make it work when the name of the fields it is supposed to add up are php arrays.


    Javascript :
    Code:
    <script> 
    function total(){ 
    var vtemp=0 
    vresult=0 
    for (var i=1;i<27;i++){// loop to get all inputs 
    vtemp=eval("parseFloat(document.all.ammount"+i+".value)") 
    if(!isNaN(vtemp)) vresult+=vtemp// if value is a number - add it. 
    } 
    document.all.total.value=vresult// change the last input...Result one 
    } 
    </script>
    Okay, so normally the field "ammount" would be "ammount1, ammount2, etc..", but on this paticular form I need it to be "ammount[id of the item], ammount[id of another item], etc..".

    I am not sure how to make the javascript I have work with the php array...

  2. #2
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Amount has one 'm'.

    What does 'ammount[id of the item]' mean?
    Code:
    function total()
    { 
    	var i = 1, vtemp = 0, vresult = 0;
    	for (i; i < 27; i++)
    	{
    		vtemp = parseFloat(document.forms[0]['amount' + i].value);
    		if (!isNaN(vtemp))
    			vresult += vtemp; 
    	} 
    	document.forms[0].total.value = vresult;
    }
    [correction of the other one]
    ::: certified wild guess :::

  3. #3
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    'amount[id of the item]' means like in the database an item has an id of 7 so it will show name="amount[7]" in the html.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 
        "http://www.w3.org/TR/html4/loose.dtd">
    
    <html>
    <head>
    <title>untitled</title>
    <script type="text/javascript">
    
    var item_ids = [ '7' , '11' , '23' , '30' , '38' ];
    
    function addem(oForm)
    { 
    	var i = 0, vtemp = 0, vresult = 0;
    	for (i; i < item_ids.length; i++)
    	{
    		vtemp = Number(oForm.elements['amount' + item_ids[i]].value);
    		if (!isNaN(vtemp))
    			vresult += vtemp;
    		else oForm.elements['amount' + item_ids[i]].value = ''; //optional, clears invalid entry
    	} 
    	oForm.elements.total.value = vresult;
    }
    
    </script>
    </head>
    <body>
    <form>
    <input type="text" name="amount7" value="" onkeyup="return addem(this.form)" />__amount7<br />
    <input type="text" name="amount11" value="" onkeyup="return addem(this.form)" />__amount11<br />
    <input type="text" name="amount23" value="" onkeyup="return addem(this.form)" />__amount23<br />
    <input type="text" name="amount30" value="" onkeyup="return addem(this.form)" />__amount30<br />
    <input type="text" name="amount38" value="" onkeyup="return addem(this.form)" />__amount38<br /><br />
    <input type="text" name="total" value="0" readonly="readonly" />_________sum<br /><br />
    <input type="button" value="add 'em" onclick="return addem(this.form)">
    </form>
    </body>
    </html>
    Just have PHP echo into the document the ids (blue). Added onkeyup handlers; use either these or the button, depending on what you like better. Don't use the same name for the function & any form field ! (scoping issue)

    If this: name="amount[7]" ...is the actual HTML, change the above to:
    Code:
    var item_ids = [ '[7]' , ...etc.
    ::: certified wild guess :::

  5. #5
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a bunch! it works great!!:-D


Bookmarks

Posting Permissions

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