blog | bio | agenda | jobs | ajaxCFC

ajaxCFC Update: addOptions() - serializeForm()

I just updated the ajaxCFC distribution files to include an update to the addOptions() utility. The y! group has been talking about passing a CF Query to the addOptions() function for a few weeks and thanks to the contribution of Jeff Lester it's possible now. It behaves exactly like the addRows() function, where it takes _cell_ functions to get the value and text of the returned query. It is usually easier to learn by example, so check out how it's done.

Additionally, some of you must be familiar and frustrated with the lack of ability of passing an entire form as an object; if you try, you'd get an maximum recursion error. I wrote a small function in my contact manager example that takes a form and serialize it into a JavaScript object, so I chose to include it into the DWRUtil namespace... You can now call x = DWRUtil.serializeForm(document.frm) and pass it to your Ajax call.

TrackBacks
There are no trackbacks for this entry.

Trackback URL for this entry:
http://www.robgonda.com/blog/trackback.cfm?C5090FB5-3048-7431-E400742948EF3856

Comments
ohh, I really love the DWRUtil.serializeForm(document.frm) .... very convenient and straight forward, thank for the great work....
# Posted By Daniel | 6/12/06 9:20 AM
Your serializeForm is missing options for textarea and multi-select boxes (select-multiple). The textarea you can just add to your main list (text, select-one,hidden,radio,textarea). The select-multiple needs special handling similar to the checkbox. I can send you the code if you like. Can I paste it here?

ajaxCFC rules!
# Posted By Stuart MacAdam | 6/14/06 9:42 PM
Stuart,

Thanks, you're right. Should be easy, but if you already have it you can paste it here or drop me an email. rob[at]robgonda[dot]com

Cheers.
# Posted By Rob Gonda | 6/14/06 10:39 PM
Here it is. The passing in of "vals" is just something I added that lets me append the fields from additional forms on the same page.


DWRUtil.serializeForm = function(frm,vals) {
   if (!vals) {
      var vals = {};
   }
   for (var loop=0; loop < frm.elements.length; loop++) {
      if (frm.elements[loop].type == 'text' || frm.elements[loop].type == 'select-one' || frm.elements[loop].type == 'hidden' || frm.elements[loop].type == 'radio' || frm.elements[loop].type == 'textarea') {
         vals[frm.elements[loop].name] = frm.elements[loop].value;
      }
      else if (frm.elements[loop].type == 'checkbox') {
         if (frm.elements[loop].checked == true) {
            if (vals[frm.elements[loop].name]) {
               vals[frm.elements[loop].name] = vals[frm.elements[loop].name] + ',' + frm.elements[loop].value;
            } else {
               vals[frm.elements[loop].name] = frm.elements[loop].value;
            }
         }
      }
      else if (frm.elements[loop].type == 'select-multiple') {
         var ob = frm.elements[loop];
         for (var i = 0; i < ob.options.length; i++) {
          if (ob.options[i].selected) {
               if (vals[frm.elements[loop].name]) {
                  vals[frm.elements[loop].name] = vals[frm.elements[loop].name] + ',' + ob.options[i].value;
               } else {
                  vals[frm.elements[loop].name] = ob.options[i].value;
               }
            }
         }   
      }         
   }
return vals;
}
# Posted By Stuart MacAdam | 6/14/06 10:46 PM
that works, but I should have cleaned it up a bit... this is better:

DWRUtil.serializeForm = function(frm,vals) {
   if (!vals) {
      var vals = {};
   }
   for (var loop=0; loop < frm.elements.length; loop++) {
      var ob = frm.elements[loop];
      if (ob.type == 'text' || ob.type == 'select-one' || ob.type == 'hidden' || ob.type == 'radio' || ob.type == 'textarea') {
         vals[ob.name] = ob.value;
      }
      else if (ob.type == 'checkbox') {
         if (ob.checked == true) {
            if (vals[ob.name]) {
               vals[ob.name] = vals[ob.name] + ',' + ob.value;
            } else {
               vals[ob.name] = ob.value;
            }
         }
      }
      else if (frm.elements[loop].type == 'select-multiple') {
         for (var i = 0; i < ob.options.length; i++) {
          if (ob.options[i].selected) {
               if (vals[ob.name]) {
                  vals[ob.name] = vals[ob.name] + ',' + ob.options[i].value;
               } else {
                  vals[ob.name] = ob.options[i].value;
               }
            }
         }   
      }         
   }
return vals;
}
# Posted By Stuart MacAdam | 6/14/06 11:03 PM
I'd love to see a working example of this.
# Posted By Mark | 6/16/06 3:18 PM
Ah, now I get it. This IS cool!

<form onsubmit="doFormTest(this);return false;">
<input type="Text" name="f1">
<input type="Text" name="f2">
<input type="submit" value="enter">
</form>

function doFormTest(frm) {
// send data to CF
DWREngine._execute('myFormTest.cfc', null, 'echo', DWRUtil.serializeForm(frm) , doFormTestResult);
}

---

<cffunction name="echo" output="no" access="private">
   <cfargument name="formVars" required="Yes" type="struct">
   <cfreturn structkeylist(arguments.formVars) />
</cffunction>
# Posted By Mark | 6/16/06 3:59 PM
Mark,

I linked an example of addOptions in the blog post, and as far as the serialize form, all contact manager examples for the model glue and machii frameworks are using it. I can write a simpler one if you wish.

~Rob
# Posted By Rob Gonda | 6/16/06 5:00 PM
Hehe, precisely. I may even add that one as the simple example.

Thanks
# Posted By Rob Gonda | 6/16/06 5:03 PM
DWRUtil.serializeForm has a bug in with respect to radio buttons... it doesn't grab only the selected one. The snippet below, added to the if/else block (after removing 'radio' from the first "if") fixes this:

else if ((ob.type == 'radio') && (ob.checked == true)) {
vals[ob.name] = ob.value;
}
# Posted By Mark | 6/16/06 5:25 PM
I have tried to use the DWRUtil.serializeForm() method and it simply doesn't work.

The are two form fields (doctitle & docduedate). I call the javascript with postForm(this) from the onSubmit() event. I can get the value using frm.doctitle.value, but when I call DWRUtil.serializeForm(frm), it generates a blank string.

What am I missing here, anyone?
# Posted By Kevin S. Anderson | 9/1/06 3:26 PM
Stuart and Mark,

Thanks for the suggestion; I will probably incorporate those changes in my next release.
# Posted By Rob Gonda | 9/9/06 10:14 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.1.004.