setting from values

D

David Groom

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?
 
B

Berislav Lopac

David said:
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
 
M

Michael Winter

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?

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
 
D

David Groom

Mike
Many thanks, just what I was looking for.

David


Michael Winter said:
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?

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top