Summing up fields from a select box

Discussion in 'JavaScript' started by fyremoon, Aug 31, 2011.

  1. #1
    Hello,

    I have a select box that I would like to extract the fields from and create a total from them.

    
    <select class="mydds" name="player_$userid_$i" style="width:50px">
    <option value="blue">Blue</option>
    <option value="red">Red</option>
    </select>
    
    HTML:
    I also need to pass a grade along with the player, so would I put this in an id field?

    I basically need to write a JavaScript function that totals up the number of red players and place that in a text box followed by the number of blue players into another text box. I also need to work out the total of the grades of red players and place that in a text box and the total of the grades of the blue players and place that in the last text box.

    I hope someone can post a possible solution or can offer some assistance.

    Best,

    John
     
    fyremoon, Aug 31, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    How many select boxes do you have on one page? Where are you keeping the grades? Cookies? A database? (You can't persist data in the Javascript, since any variables you change go back to what they were when the player comes to the site next time.)
     
    Rukbat, Aug 31, 2011 IP
  3. fyremoon

    fyremoon Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The site is PHP driven, so for each event the number of players (where $userid is the id of a player) will be different and the number of games played (where $i is the game number) are also different.

    So a possible select box could be name="player_1234_9" and the form has a hidden field of the total number of games to be played. The PHP script in receipt of the form has a for loop to iterate from 1 to number of games played and a while loop extracts players from a table of a database to pair the POST variable and extract the value.

    You could have 10, you could have 200 the system is dynamic.

    The javascript itself is just used to show a real time value of how many select boxes are set to blue that correspond to the value of $i.
     
    fyremoon, Aug 31, 2011 IP