fusiongrokker

Entries for month: April 2009

Chrome/Safari Gotcha: Nameless Form Fields

In ColdFusion, you can loop over the keys in a structure without knowing what the key names are, by using either this syntax:

<cfloop collection="#myStruct#" item="key">
    <cfset foo = myStruct[key] />
</cfloop>

Or this syntax:

<cfscript>
    for (var key in myStruct){
        foo = myStruct[key];
    }
</cfscript>

This is useful when you need to loop over a form with dynamically created fields — like editing N person records at a time.

I recently found out that in Webkit — and thus Safari and Google Chrome — form fields without a name attribute are included in a form post, just without a name. Odd, right? I know. I've used nameless form fields in the past, like a select box that's only used for UI functionality, tied to JavaScript. In IE and Firefox, the field is ignored and not posted with the form. In Chrome and Safari, though? Included! Let's look at an example form.

<form action="" method="post">
    <select id="test">
        <option value="foobar" selected="selected">Foobar</option>
    </select>
    <input type="submit" name="submitBtn" value="Submit" />
</form>

<cfif structKeyExists(form, "submitBtn")>
    <cfdump var="#form#">
</cfif>

This page will submit to itself, and if the form was submitted, dump the form scope. In Firefox, we see what you might expect:

But in Chrome or Safari, you'll notice a small difference:

The extra table row shows that there is another key in the structure; but as we can see it doesn't have a key name. I'm not sure why the value isn't displaying, because it's there. I'm guessing it's got something to do with the null key name. (I'm filing this whole thing under odd but true.)

So if you attempt to loop over each key and output it, depending on your method, you'll run into an error. I'm personally a fan of the structName[keyName] notation, so when I first tried this, no error was thrown, and my output was:

<cfif structKeyExists(form, "submitBtn")>
    <cfloop collection="#form#" item="key">
        <cfoutput>#key#=#form[key]#</cfoutput><br/>
    </cfloop>
</cfif>

Outputs:

=foobar
SUBMITBTN=Submit
FIELDNAMES=SUBMITBTN

I know that not having a key name can cause problems though. Let's try outputting each value using evaluate instead of structName[keyName] notation.

<cfif structKeyExists(form, "submitBtn")>
    <cfloop collection="#form#" item="key">
        <cfoutput>#key#=#evaluate("form." & key)#</cfoutput><br/>
    </cfloop>
</cfif>

Now, I'm always saying there's almost never a good excuse for using evaluate because there's almost always a more efficient way around it. For that reason, I can't think of what you might be doing that would put you in this situation… but it's still best that you're aware of the potential issue.

My advice would be to either try to put the nameless form fields outside of a form (which would be considered malformed XHTML), or to just give it a name and ignore it in your processing. The real lesson here is not to ever rely on browser quirks, because they may not exist in your favor forever.

Posted in Best Practices | ColdFusion | 3 Responses April 29 2009

Scribe 1.1.2

Just a minor update for Scribe today. I've added support for the use of {authorEmail} as a variable in the "from" email address field on the settings page. If you enter {authorEmail}, the subscription emails will appear to be sent from the post author's email address.

I welcome any and all feature requests. :)

Plugin:
Scribe
Version:
1.1.2
Requires:
Mango Blog 1.3+
Auto-install URL:
http://fusiongrokker.com/get/Scribe

Posted in Mango | My projects | 10 Responses April 22 2009

Scribe 1.1

As I mentioned when I originally announced the plugin, I've been working on some updates for Scribe, enough to call it 1.1. Here's what's changed:

Plugin:
Scribe
Version:
1.1
Requires:
Mango Blog 1.3+
Auto-install URL:
http://fusiongrokker.com/get/Scribe

Posted in Mango | My projects | No Responses Yet April 16 2009

Related Entries 1.1

I've updated my Related Entries Mango Plugin to version 1.1, which just adds Railo support. I could've sworn I had already done this, but apparently not… since Dave let me know today that it wouldn't work for him and it turned out to be a Railo incompatibility. If you're not running on Railo, then there's no reason to upgrade other than to stay at the current version. And I doubt you were already running it unmodified on Railo, because it wouldn't have worked, so…

Plugin:
Related Entries
Version:
1.1
Requires:
Mango Blog 1.3+
Auto-install URL:
http://fusiongrokker.com/get/RelatedEntries

Posted in Mango | My projects | 1 Response April 16 2009