SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  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 Very Simple Question

    I am sure this is very simple to do, but I can't figure it out so here goes..

    I have a form, and I want to run two functions onKeyUp, but I don't know how to have it run both...


    I was trying something like this...but it didn't work obviously...
    Code:
    <input type="text" name="amount[5]" size="4" maxlength="4" class="forms" onkeyup="return total(this.form);return canget(this.form)">

  2. #2
    SitePoint Wizard silver trophy rushiku's Avatar
    Join Date
    Dec 2003
    Location
    A van down by the river
    Posts
    2,056
    Mentioned
    0 Post(s)
    Tagged
    1 Thread(s)
    Only of your methods may return a value, the second one if you want both to run.

    Here's what you've written:

    onQuestion='return yes; return no;'

    point to ponder: if the statement could execute, what would be the state of onQuestion?

  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)
    Well I know what I have is wrong, but are you trying to say what I am trying to do isn't possible?

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about calling total(), then have total() call canget().

  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)
    oh okay, so like :

    Code:
    function total(oForm)
    { 
     ...all the stuff...
    return can_get();
    }
    or something like that? or would I need to put in the field names that it applies to?

  6. #6
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, what you got would work.

    What do you mean "field names that it applies to"? I saw your other post about total(), so I know what that does. I am just not sure what canget() does.

  7. #7
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well look at my new post, "Subtracting Certain Form Fields...". I decided to try and just merge the two functions becaus canget wasn't even working right anyway..but now neither works merged=/

  8. #8
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is starting to look suspiciously like homework.

    schooglepets...not a chat here: please don't start new posts on the same topic. Why make what you're doing hard to follow?
    Don't use the same name for the function & any form field ! (scoping issue)
    'Nuff said.

    Where are the 'canget' values - the maximum allowed for each item - coming from? You need to have something to subtract from. Post a better description.
    ::: certified wild guess :::

  9. #9
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay sorry...I changed some stuff in it some more, but still doesn't seem to work :

    Code:
    function total(oForm)
    { 
     var i = 0, vtemp = 0, vresult = 0, vresult2 = 0;
     for (i; i < item_ids.length; i++)
     {
      vresult2 = Number(oForm.elements['can_get' + i].value)
      vtemp = Number(oForm.elements['amount' + item_ids[i]].value);
      if (!isNaN(vtemp))
       vresult += vtemp;
       vresult2 -= vtemp;
      else oForm.elements['amount' + item_ids[i]].value = ''; //optional, clears invalid entry
     } 
     oForm.elements.total2.value = vresult;
     oForm.elements['can_get' + i].value = vresult2; 
    }

  10. #10
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ROFL!!!!! Yeah, this kinda stink of homework, but you beat me to the punch

    I am also confuse where "canget" comes from, and it is very difficult keeping up with all the different threads.

  11. #11
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay maybe this will help...the page I am working on this is http://apexwebgaming.com/ek/test.php that should show what I am trying to do, and you won't miss where the can get is.

  12. #12
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Change 'can_get' to 'canget', because your variables in the HTML was declared without the underscore.

  13. #13
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm true, but it still doesn't work.

  14. #14
    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>
    <script type="text/javascript">
    
    function total(oAmountField)
    {
    	if (isNaN(Number(oAmountField.value)))
    	{
    		oAmountField.value = '';
    		return false;
    	}
    	else
    	{
    		var amount_val, amount_idx, can_get_field, can_get_val, subtotal = 0, oForm = oAmountField.form;
    		amount_idx = oAmountField.name.replace(/[^\d]/g, ''); //get n of 'amount[n]' field
    		can_get_field = oForm.elements['canget[' + amount_idx + ']']; //paste into 'canget[n]', get other field
    		amount_val = Number(oAmountField.value); //get 'want'
    		can_get_val = Number(can_get_field.defaultValue); //get 'can get' (original limit)
    		if (can_get_val - amount_val >= 0) //available?
    			can_get_field.value = can_get_val - amount_val; //OK, subtract
    		else
    		{
    			alert('Too many.');
    			oAmountField.value = '';
    			can_get_field.value = can_get_field.defaultValue; //restore
    			return false;
    		}
    	}
    	var el, i = 0;
    	while (el = oForm.elements[i++]) //loop through form
    		if (/amount\[\d+\]/.test(el.name)) //element name="amount[n]" ?
    			subtotal += Number(el.value); //sum
    	oForm.elements.total2.value = subtotal; //out
    }
    
    </script>
    </head>
    <body>
    <form action="http://eternalkingdoms.com/process_buildings.php" method="post">
    <table border="2" bordercolor="black" cellpadding="5" cellspacing="0" width="90%" align="center">
    <tr>
    <td align="left" width="20%">
    <b>Building</b>
    </td>
    <td align="left">
    <b>Want</b>
    </td>
    <td align="left">
    <b>Have</b>
    </td>
    <td align="left">
    <b>Can Get</b>
    </td>
    <td align="left">
    <b>Gold</b>
    </td>
    <td align="left">
    <b>Stone</b>
    </td>
    <td align="left">
    <b>Wood</b>
    </td>
    <td align="left">
    <b>Food</b>
    </td>
    <td align="left">
    <b>Turns</b>
    </td>
    <td align="left">
    <b>Peasents</b>
    </td>
    </tr><tr>
    <td background="images/menu_bg.gif" align="left" width="10%">
    Wood Wall</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="amount[1]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="canget[1]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    10</td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    1</td>
    <td background="images/menu_bg.gif" align="left">
    2</td>
    </tr><tr>
    <td background="images/menu_bg2.gif" align="left" width="10%">
    <font color="#000000">Small Watch Tower</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="amount[2]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="canget[2]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">10</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">15</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">10</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">2</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">5</font></td>
    </tr><tr>
    <td background="images/menu_bg.gif" align="left" width="10%">
    Barracks</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="amount[4]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="canget[4]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg.gif" align="left">
    25</td>
    <td background="images/menu_bg.gif" align="left">
    100</td>
    <td background="images/menu_bg.gif" align="left">
    75</td>
    <td background="images/menu_bg.gif" align="left">
    50</td>
    <td background="images/menu_bg.gif" align="left">
    5</td>
    <td background="images/menu_bg.gif" align="left">
    7</td>
    </tr><tr>
    <td background="images/menu_bg2.gif" align="left" width="10%">
    <font color="#000000">Stone Mine</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="amount[9]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="canget[9]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">50</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">50</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">25</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">1</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">12</font></td>
    </tr><tr>
    <td background="images/menu_bg.gif" align="left" width="10%">
    Gold Mine</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="amount[10]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="canget[10]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    50</td>
    <td background="images/menu_bg.gif" align="left">
    100</td>
    <td background="images/menu_bg.gif" align="left">
    50</td>
    <td background="images/menu_bg.gif" align="left">
    1</td>
    <td background="images/menu_bg.gif" align="left">
    15</td>
    </tr><tr>
    <td background="images/menu_bg2.gif" align="left" width="10%">
    <font color="#000000">Saw Mill</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="amount[11]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="canget[11]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">50</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">50</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">25</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">1</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">11</font></td>
    </tr><tr>
    <td background="images/menu_bg.gif" align="left" width="10%">
    House</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="amount[12]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg.gif" align="left">
    0</td>
    <td background="images/menu_bg.gif" align="left">
    <input type="text" name="canget[12]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg.gif" align="left">
    30</td>
    <td background="images/menu_bg.gif" align="left">
    50</td>
    <td background="images/menu_bg.gif" align="left">
    50</td>
    <td background="images/menu_bg.gif" align="left">
    25</td>
    <td background="images/menu_bg.gif" align="left">
    1</td>
    <td background="images/menu_bg.gif" align="left">
    2</td>
    </tr><tr>
    <td background="images/menu_bg2.gif" align="left" width="10%">
    <font color="#000000">Drink House</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="amount[15]" size="4" maxlength="4" class="forms" onkeyup="return total(this)">
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">0</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <input type="text" name="canget[15]" size="4" maxlength="4" value="3" class="forms" readonly>
    </td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">50</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">25</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">45</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">5</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">2</font></td>
    <td background="images/menu_bg2.gif" align="left">
    <font color="#000000">3</font></td>
    </tr><tr>
    <td align="left">
    <input type="submit" value="Construct Buildings" class="forms">
    </td>
    <td align="left">
    <input type="text" name="total2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <br>
    </td>
    <td align="left">
    <br>
    </td>
    <td align="left">
    <input type="text" name="total_gold2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <input type="text" name="total_stone2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <input type="text" name="total_wood2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <input type="text" name="total_food2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <input type="text" name="total_turns2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    <td align="left">
    <input type="text" name="total_peasents2" size="4" maxlength="4" value="0" class="forms" readonly>
    </td>
    </tr>
    </table>
    </form>
    </body>
    </html> 
    
    ::: certified wild guess :::

  15. #15
    SitePoint Wizard GoldFire's Avatar
    Join Date
    Oct 2002
    Location
    Oklahoma City, OK
    Posts
    1,517
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't thank you enough


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
  •