Link to home
Start Free TrialLog in
Avatar of gamebits
gamebitsFlag for Canada

asked on

javascript simple calculation between text box

Easy points for a javascript Guru. I know this is not what EE is for I should research a script and come up with the problems I have to implement it but I feel this is probably simple enough for a Guru to write it up from scratch rather than patch an existing script. If you guys feel differently let me know and I will come up with something

I need a simple calculation script add and substract between the fields

It is basically to perform maintenance in baseball card collection

There is 4 fields here is how they should interact

  Owned and Trade is the total of that card owned by the user so the number entered in Trade need to be substracted from Owned the reverse is also true
 
  A user can add more card in Owned after the Trade is down to zero i.e Owned 15 Trade 5 Total 20 cards if the user want to add a card to his inventory (to make it 21) adding a card to Owned should only move a card from Trade to Owned (owned 16 - trade 4) to increase the total to 21 it should be Owned 21 Trade 0

  Delete: the number of card entered in this box need to be substract from Trade and from Owned if there was not enough in the Trade column
 
  Wanted: stand alone, number can be edited without changing other fields
 
The best of the best would be to use AJAX to update the database as we go but I can live with a submit button to an update script.

The list is populated from a mysql database there is a small sample on how it is produced/displayed
The script
<?php
include 'CONNECT TO DB';
 
$sql = mysql_query("SELECT * FROM My_Cards WHERE user_id = '1' AND set_id = '262'");
 
         echo "<form action=\"update_mycards.php\" method=\"POST\">
               <table border=1 width=\"500\">
                <tr>
                   <th>Card ID:</th>
                   <th>Owned</th>
                   <th>Wanted</th>
                   <th>Trade</th>
                   <th>Delete</th>
                </tr>";
                              $i = 1;
      while($data = mysql_fetch_array($sql)){
        
              $query = mysql_query("SELECT card_title FROM Non_Sports WHERE card_id = '$data[card_id]'");
 
                    while($row = mysql_fetch_array($query)){
                      
                        $title = $row['card_title'];
                        $title = stripslashes(strip_tags($title));
 
           echo "<tr>
                    <td>$title</td>  <td><input type=\"text\" name=\"owned$i\" size=\"5\" value=\"$data[owned]\"></td>  <td><input type=\"text\" name=\"wanted$i\" size=\"5\"  value=\"$data[wanted]\"></td>  <td><input type=\"text\" name=\"trade$i\" size=\"5\" value=\"$data[trade]\"></td> <td><input type=\"text\" name=\"delete$i\" size=\"5\"></td>
                </tr>"; $i++;
      }
      }
           echo "</form>
                 </table>";
 
?>

Open in new window

screen-shot.jpg
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Sounds like homework to me
Avatar of gamebits

ASKER

Sure sounds like it but it's not, I'm 51 years old and no plan to go back to school. You can email me if you'd like to see the application I'm working on.
51 and collecting baseball cards ? Hmmm (only kidding, I know they can be valuable)

Sure I can give you a hand.

Have a look in my profile for my email
gamebits: With Michel, you have the right guy working on this, but I just had to add my two cents.  

My son's baseball team is coached by the grandfather of one of the players.  He is 74 years old and he not only collect baseball cards, he goes to Florida every year for Spring Training so he can get easy access to the players for autographs!

Best to all, ~Ray
I'll have a look in a few hours. Just got up.
Assuming you give DELETE an ID of xc where x is 1,2,3 like this
<td><input type="text" name="delete" id="1c" size="5"></td>



 
      
      window.onload = function(e) {
         var lInputs = document.getElementsByTagName("input");
      
         for (var i = 0; i < lInputs.length; i++) {
            if (lInputs[i].id)  lInputs[i].onkeyup = function() {
                  var id = parseInt(this.id);
               var owned = document.getElementById(id+'a');
               var trade = document.getElementById(id+'b');
               var delet = document.getElementById(id+'c');
               var tradeVal = parseInt(trade.value);
               if (isNaN(tradeVal)) tradeVal = 0;
               var deleteVal = parseInt(delet.value);
               if (isNaN(deleteVal)) deleteVal = 0;
               var ownedVal = owned.defaultValue;
               if (isNaN(ownedVal)) ownedVal = 0;
               ownedVal = ownedVal-tradeVal-deleteVal
               if (ownedVal < 0) alert('You do not have that number of cards')
               else owned.value=ownedVal;
 
            }
         }
      }

Open in new window

Thank you very much for this m. Plungjan not exactly what I had envisioned in the first place but definitely workable, I see one problem though and it is my bad, the problem is when there is data already in the trade column the script doesn't work the way it should.

For instance when you start with 20 in the owned column and 0 in the trade everything works fine, but if you have card in the trade column already when you open the page (this is populated from the database) then the script doesn't take into account that the total number of cards is actually what is in the owned column + what is in the trade column.

@Ray - You are right Michel is definitively the right guy to help me out on this, as far as baseball players autograph that is a good thing to know :)
Yeah, I already thought of an onload recalc in case you reload the page or return from elsewhere
I will get you something tomorrow
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Although not exactly what I was looking for, the solution is technically correct.