Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > setting from values

Reply
Thread Tools

setting from values

 
 
David Groom
Guest
Posts: n/a
 
      11-22-2004
I have a form which contains amongst other code:


<form action="" name="diagform" id="diagform">
<input name="answer" type="hidden" id="q1" value="">
<input name="answer" type="hidden" id="q2" value="">
<input name="answer" type="hidden" id="q3" value="">
........
<input name="answer" type="hidden" id="q9" value="">
<form>

and I have the following javascript function:

function SetAnswer(quest,ans) {
var quest,ans;
document.diagform.answer[quest].value=quest+'.'+ans;
}


This works as I expect it, that is to say that when SetAnswer is called as
SetAnswer(q1,1) the value of answer gets set to 1 for the first hidden
field,etc.

However when the form is submitted I want all the values of answer to be in
an array for processing in PHP.

To do that I have to change the input names to "answer[]".

I can't now work out how to set the value of that element using javascript.

Anyone got any ideas?





 
Reply With Quote
 
 
 
 
Berislav Lopac
Guest
Posts: n/a
 
      11-22-2004
David Groom wrote:
> I have a form which contains amongst other code:
>
>
> <form action="" name="diagform" id="diagform">
> <input name="answer" type="hidden" id="q1" value="">
> <input name="answer" type="hidden" id="q2" value="">
> <input name="answer" type="hidden" id="q3" value="">
> .......
> <input name="answer" type="hidden" id="q9" value="">
> <form>
>
> and I have the following javascript function:
>
> function SetAnswer(quest,ans) {
> var quest,ans;
> document.diagform.answer[quest].value=quest+'.'+ans;
> }
>
>
> This works as I expect it, that is to say that when SetAnswer is
> called as SetAnswer(q1,1) the value of answer gets set to 1 for the
> first hidden field,etc.
>
> However when the form is submitted I want all the values of answer to
> be in an array for processing in PHP.
>
> To do that I have to change the input names to "answer[]".
>
> I can't now work out how to set the value of that element using
> javascript.
>
> Anyone got any ideas?


Instead of form.answer[] (which wouldn't work, use form['answer[]'].

Berislav


 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      11-22-2004
On Mon, 22 Nov 2004 13:38:45 +0100, Berislav Lopac
<> wrote:

> David Groom wrote:


[snip]

>> function SetAnswer(quest,ans) {
>> var quest,ans;


Delete that statement. The arguments, quest and ans, are already local
variables.

>> document.diagform.answer[quest].value=quest+'.'+ans;


So you append the answer after the identifier of the question?

>> }
>>
>> This works as I expect it, that is to say that when SetAnswer is called
>> as SetAnswer(q1,1)


I assume you mean

SetAnswer('q1', '1') or SetAnswer('q1', 1)

[snip]

>> [...] I have to change the input names to "answer[]".


[snip]

>> Anyone got any ideas?

>
> Instead of form.answer[] (which wouldn't work, use form['answer[]'].


formObj.elements['answer[]']

would be better, however, you can reference the controls directly via
their id:

function setAnswer(q, a) {
document.forms['diagform'].elements[q].value = q + '.' + a;
}

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
David Groom
Guest
Posts: n/a
 
      11-22-2004
Mike
Many thanks, just what I was looking for.

David


"Michael Winter" <> wrote in message
newspshvbhvrnx13kvk@atlantis...
> On Mon, 22 Nov 2004 13:38:45 +0100, Berislav Lopac
> <> wrote:
>
> > David Groom wrote:

>
> [snip]
>
> >> function SetAnswer(quest,ans) {
> >> var quest,ans;

>
> Delete that statement. The arguments, quest and ans, are already local
> variables.
>
> >> document.diagform.answer[quest].value=quest+'.'+ans;

>
> So you append the answer after the identifier of the question?
>
> >> }
> >>
> >> This works as I expect it, that is to say that when SetAnswer is called
> >> as SetAnswer(q1,1)

>
> I assume you mean
>
> SetAnswer('q1', '1') or SetAnswer('q1', 1)
>
> [snip]
>
> >> [...] I have to change the input names to "answer[]".

>
> [snip]
>
> >> Anyone got any ideas?

> >
> > Instead of form.answer[] (which wouldn't work, use form['answer[]'].

>
> formObj.elements['answer[]']
>
> would be better, however, you can reference the controls directly via
> their id:
>
> function setAnswer(q, a) {
> document.forms['diagform'].elements[q].value = q + '.' + a;
> }
>
> Mike
>
> --
> Michael Winter
> Replace ".invalid" with ".uk" to reply by e-mail.



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 2 08-22-2006 09:30 PM
setting Onclick and setting the ClientID in behind code isn't working in Mozilla davidr@sharpesoft.com ASP .Net 0 08-21-2006 11:55 PM
python-dev and setting up setting up f2py on Windows XP Sile Python 5 08-18-2006 08:13 AM
tomcat 4.x : setting mime type for a directory or setting a default mime type CJ Java 1 10-29-2004 07:51 PM
Setting a configurable time-out setting for all the communication initiated with Remoting object. Srinivasa Raghavan Sethuraman ASP .Net 0 06-30-2004 10:05 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57