Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript simple calculation between text box

Posted on 2009-05-03
10
Medium Priority
?
380 Views
Last Modified: 2013-12-12
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
0
Comment
Question by:gamebits
  • 6
  • 3
10 Comments
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24292770
Sounds like homework to me
0
 
LVL 28

Author Comment

by:gamebits
ID: 24294339
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.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24296279
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 24300162
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
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24301467
I'll have a look in a few hours. Just got up.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24302006
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

0
 
LVL 28

Author Comment

by:gamebits
ID: 24303936
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 :)
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 24304176
Yeah, I already thought of an onload recalc in case you reload the page or return from elsewhere
I will get you something tomorrow
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 2000 total points
ID: 24312564
Here
How about this:
 
<script type="text/javascript">
function calc(id) {  
  var owned = document.getElementById(id+'a');
  var trade = document.getElementById(id+'b');
  var delet = document.getElementById(id+'c');
  if (trade) ;
  else alert(id)
  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'); /* remove if you do not want alerts */
    owned.parentNode.parentNode.style.backgroundColor="red"; /* remove parentNode.parentNode to only highlight owned */
    owned.focus();
  }
  else {
    owned.value=ownedVal;
    owned.style.backgroundColor="white";
  }
}  
window.onload = function(e) {
  var lInputs = document.getElementsByTagName("input");
  for (var i = 0; i < lInputs.length; i++) {
    var loopId = lInputs[i].id;
    if (loopId) {
      if (loopId.indexOf('a') !=-1) calc(parseInt(loopId)); // run once per line
      lInputs[i].onkeyup = function() {
        calc(parseInt(this.id));
      }
    }
  }
} 
</script>

Open in new window

0
 
LVL 28

Author Closing Comment

by:gamebits
ID: 31577337
Although not exactly what I was looking for, the solution is technically correct.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question