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.

Kevin
  • 13,153
  • 11
  • 60
  • 87

4 Answers4

8

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
  • 2,053
  • 2
  • 17
  • 35
  • 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 Jun 14 '10 at 19:26
  • ? using ajax form submit? Debug with Firebug. It shouldn't hang. – Henry Jun 14 '10 at 19:28
2

Cfwddx is also an alternative.

David Collie
  • 612
  • 3
  • 7
  • And you vote me down for that? It's a perfectly valid answer. – David Collie Jun 14 '10 at 19:55
  • 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 Jun 14 '10 at 20:03
  • 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 Jun 14 '10 at 20:08
  • 2
    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 Jun 14 '10 at 20:25
  • @rip747 wouldn't toXML() - http://cflib.org/udf/toXML better than the heavy WDDX? – Henry Jun 14 '10 at 20:30
  • @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 Jun 15 '10 at 20:56
  • @rip747 storing WDDX in XML column? That's a use case I've never really thought of. Have you tried? – Henry Jun 15 '10 at 21:15
0

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
  • 32,689
  • 19
  • 120
  • 221
0

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
  • 3,588
  • 2
  • 19
  • 14
  • Since you're using CF8, just use SerializeJSON() / DeserizlieJSON(). No need to use 3rd party library. – Henry Jun 14 '10 at 19:26
  • 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 Jun 14 '10 at 20:00