|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
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)> |
|
#6
|
|||
|
|||
|
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>
|
|
#7
|
|||
|
|||
|
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?
|
|
#8
|
|||
|
|||
|
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> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > Array passing in forms |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|