ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 30th, 2004, 11:07 AM
Zemnon Zemnon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Ukraine
Posts: 6 Zemnon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Array passing in forms

Hello guys,

I've programmed for a long time in PHP and was really suprised that I was unable to pass an array in this way in HTML forms:
<form>
<input type="text" name="array[]" value="arrayElement1"><br>
<input type="text" name="array[]" value="arrayElement1"><br>
<input type="submit" value="Submit">
</form>

It returns an array $array in PHP.
Is there any workeround of such a type in CF?

Thank you.

Reply With Quote
  #2  
Old November 30th, 2004, 12:04 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,700 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 17 h 45 m 21 sec
Reputation Power: 53
I'm not sure what you are trying to do. CF passes all the form variables together as a structure (an associative array). I'm not seeing why you would want to pass 2 text fields with the same name and the same value. Is that what you really want to do?
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian.
How to Post a Question in the Forums

Last edited by kiteless : November 30th, 2004 at 12:12 PM.

Reply With Quote
  #3  
Old December 1st, 2004, 04:03 AM
Zemnon Zemnon is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Ukraine
Posts: 6 Zemnon User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hello kiteless,


Thank you for your comment. But I have the following situation:
I have a form where javascript adds any scale of input fields. Those fields has the same name and passes to my script as an array in PHP (Example: <input type="text" name="somename[]">). But in PHP it return the elements of an array even for empty fields. But in CF it returns only the list of those that has been typed. For me is very important to keep on track the order of an arguments.

So when some fields are not filled-in, the list just passes those values out instead of an empty value.

I don't need JavaScript workeround to fill the fields with some key that I then can check in CF.

I need to know if there is a familier solution in CF as in PHP.

Reply With Quote
  #4  
Old December 1st, 2004, 08:22 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,700 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 17 h 45 m 21 sec
Reputation Power: 53
Not that I am aware of, I've never heard of such a technique. Usually when one wants to pass multiple fields, they are each named differently ("field1", "field2", "field3", etc.) and then it's a rather simple matter to loop through the form fields on the action page and handle the fields. I'd be interested to know what the HTTP spec says on the matter becuase HTTP is plain-text and thus has no concept of an "array", this must be something that PHP is doing under the hood. In other words, though I'm not positive my hunch is that it is PHP that is doing something unorthodox and not CF.

In any event, as far as I know there is no way to do what you are asking for in CF besides giving each field a unique name. You *might* be able to try playing around with the getPageContext() function to get at the HTTP header directly. But since it would be very easy to give each field a unique name I'd recommend that approach over trying to hack around it.

Reply With Quote
  #5  
Old December 1st, 2004, 10:31 AM
rivux rivux is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 6 rivux User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 8 sec
Reputation Power: 0
Its not that PHP is hacking anything, they have just created a means to reconstruct form variables back into arrays. Its a shorter way of doing what people have always done which is put form field results into arrays. PHP has it setup so that if it sees form fields with the same name it assumes its an array and builds it for you.

CF doesn't have anything like that, we would just do as kiteless said and run a loop putting the array together manually. Unfortunately CF doesn't give us the shortcut that PHP does.

You can pseudo mimic the PHP array pasing in forms by using ArrayToList and ListToArray functions. This is the quickest and easiest way to pass a one dimensional array via forms.

Lets say you have an array of member IDs, you can pass it to the form using.
<input name="members" value="#ArrayToList(members)#>

And then after submitting it you can just do the opposite

<cfset membersarray = ListToArray(form.members)>

Reply With Quote
  #6  
Old January 20th, 2005, 07:22 PM
ooglek ooglek is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 1 ooglek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I got it part of the way there...

I really like how PHP does it too. I can create multidimensional arrays and pass them back and forth in HTML forms.

I've got some code THIS far... maybe someone can figure out how to set up multidimensional hashes in Cold Fusion....

Code:
        <cfloop collection="#form#" item="i">
            #i#: #evaluate("form.#i#")#
            <cfif refind("\.",i)>
                <cfset st = refind("\.",i,1,"TRUE")>
                Array Len:#ArrayLen(st.pos)#
                <cfloop index="j" from="1" to="#ArrayLen(st.len)#">
                    #j#: pos: #evaluate("st.pos[#j#]")# len: #evaluate("st.len[#j#]")# len2: #len(i)# strip: #mid(i, evaluate("st.pos[#j#]")+1, len(i)-evaluate("st.pos[#j#]"))#
                    <cfset varname = "form2.#left(i,evaluate("st.pos[#j#]")-1)#.#mid(i, evaluate("st.pos[#j#]")+1, len(i)-evaluate("st.pos[#j#]"))#">
                    <cfset "#varname#" = evaluate("form.#i#")>
                    varname: #lcase(varname)#
                </cfloop>
            <cfelse>
                Nope.
            </cfif>
        </cfloop>


Basically loops through the submitted form, looks for at least a 2D array (form.svc.foo for example) and sets form2.svc.foo to the value. Unfortunately, I can't figure out how to loop through form2 to see if I've set them correctly... I get this:

Code:
Complex object types cannot be converted to simple values.

The expression has requested a variable or an intermediate expression result as a simple value, however, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values. <p> The most likely cause of the error is that you are trying to use a complex value as a simple one. For example, you might be trying to use a query variable in a <CFIF> tag. This was possible in ColdFusion 2.0 but creates an error in later versions.
 
The error occurred in index.cfm: line 55

53 :         </cfloop>
54 :         <cfloop collection="#form2#" item="i">
55 :             #i#: #evaluate("form2.#i#")#
56 :         </cfloop>

Reply With Quote
  #7  
Old January 20th, 2005, 07:49 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,700 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 17 h 45 m 21 sec
Reputation Power: 53
I'm not sure what you're trying to do, but that looks awfully complicated to me. Can you not just name the form fields differently? Or if you want to pass a complex value, can you not serialze it into WDDX or some other flavor of XML, pass it, and then deserialize it?

Reply With Quote
  #8  
Old January 21st, 2005, 04:28 PM
glively glively is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 17 glively User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 51 sec
Reputation Power: 0
From my limited experience with PHP, the form array[] fields are mostly used with Multi-select boxes, check boxes, and the like. It simply returns all of the values in a single field[] array. ColdFusion does this similarly, except rather than returning an array, it returns a delimited LIST of values. So if you had a multi-select box named "chocies", you could loop through the multiple choices picked by:

<cfloop index="variables.aChoice" list="#form.choices#">

</cfloop>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > Array passing in forms


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT