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.
in
Best Practices |
ColdFusion |
3 Responses
Posted 2009-04-29 11:45
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
in
Mango |
My projects |
10 Responses
Posted 2009-04-22 10:15
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:
- Support for double opt-in user validation requirement, as a new setting on the settings page.
- Subscriber management, export, and import has been added to the settings page.
- Prompt the blog administrator to visit the settings page upon activation, to configure.
- Minor bug fixes:
- Fixed display for custom event (Non-pod display wasn't displaying anything! Whoops!)
- Fixed bug: Blog default "from" email address was always used instead of user-specified override.
- Fixed bug: Unsubscribe worked across all blogs in the same database; is now blog-specific.
- Fixed bug: Signup pod wouldn't work on child pages (ie: /archives/...) because a relative path led to a 404. This has been switched to an absolute path that should work no matter where the pod is displayed.
- Plugin:
- Scribe
- Version:
- 1.1
- Requires:
- Mango Blog 1.3+
- Auto-install URL:
- http://fusiongrokker.com/get/Scribe
in
Mango |
My projects |
No Responses Yet
Posted 2009-04-16 02:36
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
in
Mango |
My projects |
1 Response
Posted 2009-04-16 01:55
Have you been waiting for Email subscriptions to be available in Mango blog? Well, wait no longer!
- Plugin version:
- 1.0
- Last Updated:
- 2009-04-08
- Requires:
- Mango Blog 1.3+, CFSchedule (if sandboxed)
- Auto-install URL:
- http://fusiongrokker.com/get/Scribe
Scribe does more than just send out some emails when you make a new post:
- ColdFusion Scheduled tasks are used to make sure emails don't go out before your post is published. Emails are only sent when a new post is published or when a draft becomes published; never when updating an existing published post or when a post is reverted from published status to draft status. (Caveat: published -> draft -> published will trigger a repeat email.)
- The email messages sent to your readers are customizable with templating on the settings page.
- Optionally use a custom email address for the FROM address. Defaults to the blog-default email address.
- There is an unsubscribe link in every email -- if you include it on your template; included by default.
- The subscribe pod is available for pod-enabled themes, but for users without a pod-enabled theme, a custom event ("scribe-pod") is also available.
I also already have some plans for a 1.1 release, in the near future:
- Subscription management in admin - view and edit the list of subscribers
- Optional double-opt-in (require link-click from validation email before subscription is active)
in
Mango |
My projects |
29 Responses
Posted 2009-04-10 08:00