Panos
asked on
Coldfusion loop through a list of pairs name - value
Hello experts.
I'm using jquery serialize function in a form in a pop up window to store temporaly the values of name and value of a form in a hidden field (myhiddenfield) in my main form.
so my hidden field has a value like name_1=value_1&name_2=valu e_2&name_3 =value_3
Submitting the form I'm taking the myhiddenfield value as a list with delimiter the & sign
<cfloop list="#myhiddenfield#" index="item" delimiters="&">
<cfset firstcolumn = listfirst(item,'=')>
<cfset secondcolumn = listlast(item,'=')>
</cfloop>
The problem i have is if there is not any value for a one key.
f.e. name_1=&name_2=value_2&nam e_3=value_ 3
I get for the first list item the same value for firstcolumn and secondcolumn
For testing i tried the listgetat function like listGetAt(item,2,'=') and i get an error Invalid list index 2 because the listitem has one element.
I have a solution using the listlen function like:
<cfif listlen(item,'=') is 2>
<cfset secondcolumn = listlast(item,'=')>
<cfelse>
<cfset secondcolumn = ''>
</cfif>
but i need your opinion if there is another way to handle this.
Thank you in advance
I'm using jquery serialize function in a form in a pop up window to store temporaly the values of name and value of a form in a hidden field (myhiddenfield) in my main form.
so my hidden field has a value like name_1=value_1&name_2=valu
Submitting the form I'm taking the myhiddenfield value as a list with delimiter the & sign
<cfloop list="#myhiddenfield#" index="item" delimiters="&">
<cfset firstcolumn = listfirst(item,'=')>
<cfset secondcolumn = listlast(item,'=')>
</cfloop>
The problem i have is if there is not any value for a one key.
f.e. name_1=&name_2=value_2&nam
I get for the first list item the same value for firstcolumn and secondcolumn
For testing i tried the listgetat function like listGetAt(item,2,'=') and i get an error Invalid list index 2 because the listitem has one element.
I have a solution using the listlen function like:
<cfif listlen(item,'=') is 2>
<cfset secondcolumn = listlast(item,'=')>
<cfelse>
<cfset secondcolumn = ''>
</cfif>
but i need your opinion if there is another way to handle this.
Thank you in advance
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Welcome :)
ASKER