After DOM manipulation, form fields won't post...

Categories: HTML/ColdFusion

This is an issue I first ran into several years ago, but I've recently helped two different people who had the same problem and thought it was worth blogging.

If you ever run into a situation where form fields step getting posted to the server after you've done some DOM manipulation, check to make sure your <form/> tag is in a valid location. If the <form/> tag resides as a direct child of either a <table/> or <tr/> tag, then you're going to have problems when the form once you've modified a child element of the <form/> tag.

This behavior does not occur in IE, but it happens in Firefox and several other browsers (I believe Safari exhibits the same behavior.)

Any of the following will break your form post submission:

<table><form><tr> .... </tr></form></table>

<table><tr><form> .... </form></tr></table>

<table><tbody><form> ... </form></tbody></table>

To correct the problem, just place the <form/> tag outside of the <table/> tag:

<form><table><tr> ... </tr></table></form>

I've usually only seen this type of markup on older sites, where I believe the intent of putting the <form/> tag inside the <table/> was to prevent any margins from being inserted into the document. However, this is invalid markup and will come back to haunt you.

NOTE: You can do <table><tr><td><form> .... </form></td></tr></table>. That's perfectly valid HTML. You just need to make sure the <form/> tag is placed somewhere were visible text is allowed.

Comments

jon martin solaas's Gravatar Thx a bunch, after googling and struggling for nearly two days I came across this post and instantly my problem was resolved. Never occurred to me that the elements were structured crappy.
Dan G. Switzer, II's Gravatar @Jon:

Glad this post helped. Thank goodness Google works fast! It's weird that I've run into several people lately who've run into this problem. I suspect it's just that more an more developers are trying to add functionality to old legacy pages.
jon martin solaas's Gravatar Well, I expected I had a pretty common problem, and I was just amazed nothing turned up on google, until your post did ... well, anyway, now Struts isn't happy to eat the new fields. I use indexed fields corresponding to an ArrayList in the DynaForm bean, and the post looks like this:

...
keywordsUnpaid[0].keywordUnpaid   fish
keywordsUnpaid[1].keywordUnpaid   bird
...

Fish was the initial value of the indexed fields, and bird has been added.

I get

Message: BeanUtils.populate
javax.servlet.ServletException: BeanUtils.populate
   at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)

As long as I don't add fields everything works just fine. I can work around it by just putting the new values comma separated in a hidden field, but it'd be nice to get it working as initially intended. Well, anyway ... that's for tomorrow ...

Regards to Nikki and Maddie, I have a 14 year old labrador - Bamse - and he's still going strong ... :-)

Add Comment



If you subscribe, any new posts to this thread will be sent to your email address.