setting from values

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Groom

    setting from values

    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.diagfo rm.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?





  • Berislav Lopac

    #2
    Re: setting from values

    David Groom wrote:[color=blue]
    > 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.diagfo rm.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?[/color]

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

    Berislav


    Comment

    • Michael Winter

      #3
      Re: setting from values

      On Mon, 22 Nov 2004 13:38:45 +0100, Berislav Lopac
      <berislav.lopac @lopsica.com> wrote:
      [color=blue]
      > David Groom wrote:[/color]

      [snip]
      [color=blue][color=green]
      >> function SetAnswer(quest ,ans) {
      >> var quest,ans;[/color][/color]

      Delete that statement. The arguments, quest and ans, are already local
      variables.
      [color=blue][color=green]
      >> document.diagfo rm.answer[quest].value=quest+'. '+ans;[/color][/color]

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

      I assume you mean

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

      [snip]
      [color=blue][color=green]
      >> [...] I have to change the input names to "answer[]".[/color][/color]

      [snip]
      [color=blue][color=green]
      >> Anyone got any ideas?[/color]
      >
      > Instead of form.answer[] (which wouldn't work, use form['answer[]'].[/color]

      formObj.element s['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.

      Comment

      • David Groom

        #4
        Re: setting from values

        Mike
        Many thanks, just what I was looking for.

        David


        "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
        news:opshvbhvrn x13kvk@atlantis ...[color=blue]
        > On Mon, 22 Nov 2004 13:38:45 +0100, Berislav Lopac
        > <berislav.lopac @lopsica.com> wrote:
        >[color=green]
        > > David Groom wrote:[/color]
        >
        > [snip]
        >[color=green][color=darkred]
        > >> function SetAnswer(quest ,ans) {
        > >> var quest,ans;[/color][/color]
        >
        > Delete that statement. The arguments, quest and ans, are already local
        > variables.
        >[color=green][color=darkred]
        > >> document.diagfo rm.answer[quest].value=quest+'. '+ans;[/color][/color]
        >
        > So you append the answer after the identifier of the question?
        >[color=green][color=darkred]
        > >> }
        > >>
        > >> This works as I expect it, that is to say that when SetAnswer is called
        > >> as SetAnswer(q1,1)[/color][/color]
        >
        > I assume you mean
        >
        > SetAnswer('q1', '1') or SetAnswer('q1', 1)
        >
        > [snip]
        >[color=green][color=darkred]
        > >> [...] I have to change the input names to "answer[]".[/color][/color]
        >
        > [snip]
        >[color=green][color=darkred]
        > >> Anyone got any ideas?[/color]
        > >
        > > Instead of form.answer[] (which wouldn't work, use form['answer[]'].[/color]
        >
        > formObj.element s['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.[/color]


        Comment

        Working...