How to script an accumulatation value on Form Submission.. for display in a View..

How to script an accumulatation value on Form Submission.. for display in a View..

I'm building an application for a sports club in which I'd like to keep track of the number of goals each player has scored each game. I've created a form with the fields - team name, opposition, date, player name, number of goals and cumulative goals.

What I'd like is when an entry is made via the form, the cumulative goal value for each player is calculated. If it's the first entry for the player then the cumulative value would be the same as the number of goals just scored. If there are previous records for the player from past games, the previous accumulated value is added to the

number of goals from the current form and the new accumulated number of goals value outputted to the cumulative goals field..

I'm trying (!) to write a script so when the form is Submitted, the calculation occurs and the respective fields are updated.

Form Name is Goals1
Field Names are Team_Name,Opposition,Player_Name,Number_of_Goals,Cumulative_Goals
I'm trying to write script in Form.. On Add.. Validate.. currently trying to use a for each loop, but it appears this adds the accumulated value to the previous entry and leaves the more recent entry blank...

if ((input.Number_of_Goals  >  0)
{
    for each vgoal in Goals1  [(Player_Name == input.Player_Name && (Cumulative_Goals is null))]
    {
        x  =  Goals1  [(Player_Name == input.Player_Name && (Cumulative_Goals is null))];
        x.Cumulative_Goals = x.Number_of_Goals;
    }
    for each vgoal in Goals1  [(Player_Name == input.Player_Name && Cumulative_Goals > 0)]
    {
        x  =  Goals1  [(Player_Name == input.Player_Name && Cumulative_Goals > 0)];
        x.Cumulative_Goals = (x.Number_of_Goals  +  x.Cumulative_Goals);
    }
}

Thanks for your time, any help or advice would be greatly appreciated..