postcoldfusioncfform

Coldfusion serializeJSON example


I'd like to see a simple template example with SerializeJSON and deserializeJSON posting to the same template and an Array adding each time a pass occurs. I've tried this several times and I keep getting confused by the pass.


Solution

  • <cfif structKeyExists(form,"jsonArray")>
        <cfset arrayData = deserializeJSON(form.jsonArray) />
        <cfset arrayAppend(arrayData,form.theText) />
    <cfelse>
        <cfset arrayData = [] />
    </cfif>
    <cfoutput>
        <form name="input" action="ableto.cfm" method="post">
            <input type="hidden" value='#serializeJSON(arrayData)#' name="jsonArray" id="jsonArray" />
            Text: <input type="text" value="" name="theText" id="theText" />
            <input type="submit" value="Subby" />
        </form>
    </cfoutput>
    
    <cfdump var="#arrayData#" />
    <cfdump var="#form#" />