Setting Form Vars Dynamically(Simple-ish)

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Setting Form Vars Dynamically(Simple-ish)

Post 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
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: Setting Form Vars Dynamically(Simple-ish)

Post 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;
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Setting Form Vars Dynamically(Simple-ish)

Post by califdon »

You might find that an array would be a lot easier to handle.
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Setting Form Vars Dynamically(Simple-ish)

Post 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 :)
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Re: Setting Form Vars Dynamically(Simple-ish)

Post by oscardog »

Anyone? :banghead:
pankajnagarkoti70
Forum Newbie
Posts: 4
Joined: Fri Sep 25, 2009 5:31 pm

Re: Setting Form Vars Dynamically(Simple-ish)

Post by pankajnagarkoti70 »

You might find that an array would be a lot easier to handle.
Post Reply