Link to home
Start Free TrialLog in
Avatar of sshivanna
sshivannaFlag for United States of America

asked on

storing serialized data

I am rendering input fields in a form which are derived from the database. The table only has two columns, name and value. Right now there are 4 rows, firstname, lastname, email and phone. More rows will be added in the next few days.

I was thinking of using the serialize() and post() api to pass the values to a coldfusion component to commit the values to the database.

But when I am calling the serialize() api, the values are going as

firstname=<value>&lastname=<value>&email=<value>&phone=<value>

Right now I can parameterize my function to receive these fields. But I would like to make it generic so that when other fields are being added, I dont have to change the function.

Can you please help?

Thank you
sshivanna
Avatar of Hammo777
Hammo777

I don't know anything about the serialize or post api but  I am curious why you would not just post normally or use ColdFusion.Ajax.submitForm?

ColdFusion.Ajax.submitForm(formId, URL[, callbackhandler, errorhandler, httpMethod, asynch])
I hope I didn't miss th point of your post.  If you really want to serialize the data you can use ColdFusion.JSON.encode to serialize javascript variables.  It has quirks though (like making the numger 1 into 1.0).  The places I have done this I submit the form normally or use ColdFusion.Ajax.submitForm and then on the back end I use <cfwddx   action = "cfml2wddx"  input="#form#" output="myvar">.  There is also the wddx javascript serializer:

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-78d6.html

Avatar of sshivanna

ASKER

Thank you @Hammo77

I am trying to build a survey using coldfusion and jquery. I am trying to use a plugin provided by jquery to create the survey tool.

The questions and answers are coming from the database. The number of questions and answers can be dynamic. Once I render the questions and answers, I need to store the responses in the database. That's why I was thinking of using the jquery serialize option so I can post as many fields as possible. Maybe there is a better way to do this.

The main challenge after this is what I explained in the previous post.

Thank you
Sharath
If you set the action of your form to action="storeSurvey.cfm" and in storeSurvey.cfm:

<!--- This will take the entire form and turn it into a serialized string --->
<!--- The form variable is an automatically built CF structure with each key being a formfield name --->
<cfwddx action="cfml2wddx" input="#form#" output="serializedForm">

<!--- store any individual fields you want and store the entire form in results column --->
<cfquery datasource="[datasource name]">
 Insert into surveyResults (firstname,lastname,formData)
VALUES(
 '#form.firstname#',
  #form.lastname#',
  '#serializedForm#'
)
</cfquery>

<h3>Thanks for doing the survey</h3>

If you want to pull the data back out:
<cfquery datasource="[datasource name]" name="userSurvey">
  SELECT formData from surveyResults
 WHERE
          firstname='Bret',
AND lastname='Farve'
</cfquery>

<cfwddx action="wddxtoCFML" input="#userSurvey.formData#" output="formData">

<cfdump var="#formData#">

ASKER CERTIFIED SOLUTION
Avatar of Proculopsis
Proculopsis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you. That works.