Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts

    how to for-loop php array ($_POST) elements correctly in js

    I wrote js functions to reset a php form (multiple questions). The below works:

    Code:
    function resetForm(){
      	   resetGroup(document.forms[0].answer1);
      	   resetGroup(document.forms[0].answer2);
    	}
    Answer1 and answer2 are the elements of $_POST array. (<input name="answer1"> in php). In this example, I tried with only two questions, but I have up to 35 questions in one form. Instead of writing the above resetGroup() 35 times, I want to for-loop it. I tried

    Code:
    function resetForm(){
    	   for (var i=1; i<3; i++){
      	   resetGroup(document.forms[0]."answer"+i);
    	}
    That didn't work. Could someone help me to make the loop work? Or would someone have better or simpler idea? Thanks.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    resetGroup(document.forms[0]["answer"+i]);

  • Users who have thanked DaveyErwin for this post:

    mondsteigen (01-16-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    resetGroup(document.forms[0].["answer"+i]); doesn't work

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Read again what Davey wrote. You didn't copy his code. You added an extra unwanted period.

    Personally, I'd avoid the repetitive references and do this:
    Code:
    function resetForm()
    {
        var form = document.forms[0];
        for (var i=1; i<3; i++)
        {
             resetGroup(form["answer"+i]);
        }
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    mondsteigen (01-16-2012)

  • #5
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks, Old Pedant. Davey's code didn't work, so I tried it with a dot (.). But I just realized that I missed the last '}' when I tried Davey's. Sorry Davey.
    Last edited by mondsteigen; 01-16-2012 at 04:26 AM. Reason: i just realized that i made a mistake.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,613
    Thanks
    80
    Thanked 4,634 Times in 4,596 Posts
    Actually, another simple trick to use is this one:
    Code:
    function resetForm()
    {
        var form = document.forms[0];
        for (var i=1; i < 1000000; i++)
        {
            var fld = form["answer"+i];
            if ( fld == null ) return;
            resetGroup( fld );
        }
    }
    Now you don't need to know ahead of time how many "answerXX" fields there are. The loop stops after the last one found.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • Users who have thanked Old Pedant for this post:

    mondsteigen (01-16-2012)

  • #7
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    3
    Thanked 0 Times in 0 Posts
    I just saw the light. Thanks Old Pedant!
    Last edited by mondsteigen; 01-16-2012 at 05:54 AM. Reason: I just saw the light.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •