Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Can I get the values from a form by using a for loop?

1. ## Can I get the values from a form by using a for loop?

I've made an "interactive" scorecard for a simulated golf game where the players can enter their scores in a form, click a button and get the totals.

The basic scorecard can be viewed here.
A more advanced one that asks for players names, and sends an alert with the winner's name can be viewed here.

I would like to make a scorecard for team play with two teams of two players per team. I want to be able to take the best (lowest) score per team for each hole and add them to find the total score. To do this with the same method means at least 72 lines of coding with all the possible errors entailed. Does anyone know of a way I can use a for loop to get the values from the form, and add only the lowest score for each team to get a total score?

• a way I can use a for loop
I rewrote your code using a for loop. Do you think that you can manage further to do whiever comparation you wanna do?

PHP Code:
``` <script language="JavaScript" type="text/javascript"> <!-- Begin function getscores(f){ var Asum = 0; var Bsum = 0; var n = 18;//number of holes for (var i=0;i<n;i++){ Asum = Asum+f.elements['pA'+(i+1)].value*1; Bsum = Bsum+f.elements['pB'+(i+1)].value*1; } f.elements['pAtotal'].value=Asum; f.elements['pBtotal'].value=Bsum; if (Asum < Bsum){ alert (player1 + "  WINS") } else if(Asum > Bsum){ alert(player2 + "  WINS") } else{alert ("IT IS A TIE")} } //  End --> </script>  ```
Note that I used a parameter to pass the form a variable, Thus don't forget to replace also in button

onclick="getscores()"

with

onclick="getscores(this.form)"

• Thanks Kor, this looks like it will work for me, will have to check it out after i get back home from work.

Nite

• Kor your code works great as seen here.