Page 1 of 1

Setting Form Vars Dynamically(Simple-ish)

Posted: Wed Sep 16, 2009 10:34 am
by oscardog
So, I have the following code:

Code: Select all

for(k = 1; k < 2; k++) { //Change to k < 3 sometime!
        for(j = 0; j < 4; j++) {
            var teamPartName = 'partnership' + j + 'total';
            var teamPartValue = document.getElementById(teamPartValue).innerHTML;
            teamPartValue = Number(teamPartValue);
            var hiddenVarNum = j + 1;
            var hiddenVarName = 't' + k + 'part' + hiddenVarNum + 'score';
            document.scoresheetform.hiddenVarName.value = teamPartValue;  
        }
    }
So basically I have hidden fields named "t1part1score" "t1part2score" etc and using a loop(above) I want to set 8 or so variables. Now I know at the momment the 'K' loop is useless, but that will be changed once I have done some other stuff(As stated by the comment). Basically instead of it trying to access 'hiddenVarName' I want it to try to access whatever is stored in the variable 'hiddenVarName'.

Think that made sense, I hope so, been playing around for a while now with no luck :(

Thanks,

Oscardog

Re: Setting Form Vars Dynamically(Simple-ish)

Posted: Wed Sep 16, 2009 11:13 am
by kaszu
Instead of

Code: Select all

document.scoresheetform.hiddenVarName
use

Code: Select all

document.scoresheetform[hiddenVarName]
Also this looks wrong

Code: Select all

var teamPartValue = document.getElementById(teamPartValue).innerHTML;
shouldn't it be getElementById(teamPartName) ?

Code: Select all

var teamPartValue = document.getElementById(teamPartName).innerHTML;

Re: Setting Form Vars Dynamically(Simple-ish)

Posted: Wed Sep 16, 2009 3:31 pm
by califdon
You might find that an array would be a lot easier to handle.

Re: Setting Form Vars Dynamically(Simple-ish)

Posted: Wed Sep 16, 2009 5:01 pm
by oscardog
kaszu wrote:Instead of

Code: Select all

document.scoresheetform.hiddenVarName
use

Code: Select all

document.scoresheetform[hiddenVarName]
Also this looks wrong

Code: Select all

var teamPartValue = document.getElementById(teamPartValue).innerHTML;
shouldn't it be getElementById(teamPartName) ?

Code: Select all

var teamPartValue = document.getElementById(teamPartName).innerHTML;
Thanks, fixed those problems but it still isn't working.

I'll post the PHP/html too:

These are the hidden fields, which are used in the JS in my first post.

Code: Select all

<input type="hidden" name="t1p1score" value="" />
<input type="hidden" name="t1p2score" value="" />
<input type="hidden" name="t1p3score" value="" />
<input type="hidden" name="t1p4score" value="" />
<input type="hidden" name="t2p1score" value="" />
<input type="hidden" name="t2p2score" value="" />
<input type="hidden" name="t2p3score" value="" />
<input type="hidden" name="t2p4score" value="" />
<input type="hidden" name="t1outs" value="" />
<input type="hidden" name="t2outs" value="" />
<input type="hidden" name="t1total" value="" />
<input type="hidden" name="t2total" value="" />
This is my submit button:

Code: Select all

<input type="submit" name="secondinnings" id="submit" value="Submit & Start Second Innings" onclick="setHiddenFields();" />
Clueless :(

(I tested it by using "echo $_POST['t1p1score'];")

Thanks :)

Re: Setting Form Vars Dynamically(Simple-ish)

Posted: Thu Sep 17, 2009 11:57 am
by oscardog
Anyone? :banghead:

Re: Setting Form Vars Dynamically(Simple-ish)

Posted: Fri Sep 25, 2009 6:11 pm
by pankajnagarkoti70
You might find that an array would be a lot easier to handle.