Professional Web Applications Themes

Array scope and a form - Macromedia ColdFusion

I'm having trouble with the scope of an array. I define it then try to populate it within a form, but once out of the form, the original values are still there. Here's a simplified example: <cfset myarray=ArrayNew(1)> <cfloop index= "i" from="1" to="5"> <cfset myarray = "a"> </cfloop> <cfif not isdefined("doit")> <form action="" method="POST" preservedata="Yes"> <cfoutput> <cfloop index="j" from="1" to="5"> <cfset myarray[j] = "bb"> input #j#: <input type="Text" name="#myarray[j]#" value="#myarray[j]#"><br> </cfloop> </cfoutput> <input type=Submit name="doit" value="Finish"> </form> <cfelse> <cfoutput> <cfloop index= "k" from="1" to="5"> output #k#: #myarray[k]#<br> </cfloop> </cfoutput> </cfif> Thanks for the help!...

  1. #1

    Default Array scope and a form

    I'm having trouble with the scope of an array. I define it then try to
    populate it within a form, but once out of the form, the original values are
    still there.

    Here's a simplified example:

    <cfset myarray=ArrayNew(1)>
    <cfloop index= "i" from="1" to="5">
    <cfset myarray = "a">
    </cfloop>

    <cfif not isdefined("doit")>
    <form action="" method="POST" preservedata="Yes">
    <cfoutput>
    <cfloop index="j" from="1" to="5">
    <cfset myarray[j] = "bb">
    input #j#: <input type="Text" name="#myarray[j]#"
    value="#myarray[j]#"><br>
    </cfloop>
    </cfoutput>
    <input type=Submit name="doit" value="Finish">
    </form>

    <cfelse>
    <cfoutput>
    <cfloop index= "k" from="1" to="5">
    output #k#: #myarray[k]#<br>
    </cfloop>
    </cfoutput>
    </cfif>

    Thanks for the help!

    arlin411 Guest

  2. #2

    Default Re: Array scope and a form

    Ok, maybe my code sample didn't really show how elementary my problem really is.

    All I want to do is populate an array in a form, then have access to the
    values created within the form. (I will be writing them to a database.)

    This should be a no-brainer, so I must have no-brain.

    (The code I posted uses a <cfif> statement to process the form on the same
    page as the form.)

    arlin411 Guest

  3. #3

    Default Re: Array scope and a form

    Output the FORM scope and see what values you receive. The array values are
    not passed by the form as such; they're embedded in the form variables. Since
    you're naming all of your text fields the same (myarray(j) will always be 'bb'
    in your form judging by the code), you should see a form variable named 'bb'
    with a value of 'bb,bb,bb,bb,bb'. Outputting the array that you populate in
    the template will always show the values you set in the template, not the
    values passed by the form. HTH,

    philh Guest

Similar Threads

  1. FORM scope and CFCs?
    By theShawn in forum Coldfusion - Advanced Techniques
    Replies: 5
    Last Post: July 20th, 04:12 PM
  2. faking a form scope
    By Ekimov in forum Coldfusion - Advanced Techniques
    Replies: 2
    Last Post: June 7th, 10:27 AM
  3. Structures from URL and FORM scope
    By Lupus 23 in forum Coldfusion - Advanced Techniques
    Replies: 4
    Last Post: March 2nd, 01:21 AM
  4. Scope problem?: Global array assignment
    By r in forum PHP Development
    Replies: 2
    Last Post: April 29th, 08:33 PM
  5. how to make a global scope array
    By Comex in forum PHP Development
    Replies: 5
    Last Post: July 26th, 02:42 PM

Bookmarks

Posting Permissions

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

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139