views:

172

answers:

4

If I wanted to save a contact form submission to the database, how can I insert the form scope in as the submission? It's been some time since I used Coldfusion.

The contact forms vary depending on what part of the site it was submitted from, so it needs to scale and handle a form with 5 fields or one with 10 fields. I just want to store the data in a blob table.

A: 

If you can't normalize the form fields into proper table(s), you can try storing them:

  • in XML (SQL Server supports XML pretty well), or
  • in JSON (in a plain varchar field), or
  • ObjectLoad() & ObjectSave() (CF9 only) to store as blob.

IIRC there are ways to get object load/save functionality in pre-CF9 by tapping into Java. http://www.riaforge.org/ or http://cflib.org/ might have it.

Henry
+4  A: 

Most space efficient way and least complicated to turn back into original shape is using serializeJSON. After that, you can use something like key:value|key:value, or XML representation of your struct.

zarko.susnjar
Okay, I used that function and was able to pass it to a CFC to insert it into my table. I can see the data in the database, but now CF sits at 'Waiting...' after I submit the form. What is causing it to hang?
Kevin
? using ajax form submit? Debug with Firebug. It shouldn't hang.
Henry
Nevermind, SMTP information is wrong on the cfmail.
Kevin
A: 

I don't know that there is a way to store a native structure into a database, but have you thought about using JSON to represent your object as key-pair values and then parsing it into a native structure after retrieving it from the database?

There are tags/functions out there that will help you with the encoding and decoding into JSON:

Joe D
Since you're using CF8, just use SerializeJSON() / DeserizlieJSON(). No need to use 3rd party library.
Henry
I agree, I merely referenced for the off-chance the asker had CF7 or lower. Otherwise, those functions are the way to go.
Joe D
+1  A: 

Cfwddx is also an alternative.

David Collie
wddx should be dead by now. :)
Henry
And you vote me down for that? It's a perfectly valid answer.
David Collie
A little strong! The fact it gives you XML could be far more useful in other server side languages, rather than this new fangled JSON :)
David Collie
I don't know, WDDX is like Flash Form. They are there for backward compatibility reason only. We should not promote the use of them anymore. Agree?
Henry
even though i side with henry that wddx shouldn't be used any more, i will say that whatever gets the job done trumps "what you are suppose to do" every time. if you're using a sql server database that have a column of xml, then in this situation i would say that using wddx would be faster and less error prone then creating your own xml transformation script.
rip747
@rip747 wouldn't toXML() - http://cflib.org/udf/toXML better than the heavy WDDX?
Henry
@henry - could be. however i would rather use wddx if you need to convert cfml types to xml as it's tried and proven. not to mention that is a native tag ;)
rip747
@rip747 storing WDDX in XML column? That's a use case I've never really thought of. Have you tried?
Henry