October 17, 2006

What NOT to Name Your Form Fields

This one is a programming post. Did you know you should never, ever, EVER name a field in your form “submit”. If you recall my previous posts, you’d recall that JavaScript can treat any old regular variable as a function. If you stupidly name your submit button “submit,” which I’ve seen done all the time, you overwrite your form’s ability to call the submit method!!!

Don’t do this:

<input type=”button” name=”submit” value=”push me” />

Or…

<input type=”text” name=”submit” value=”" />

Etc. It all messes up your JavaScript!

In other words, the following otherwise working function calls completely break (and you get cryptic errors about undefined functions or incorrect parameter counts):

document.form['form-name'].submit();
this.form.submit();
document.getElementById(’form-id’).submit();

They all fail because “submit” now refers to your form field that you created, which clearly isn’t the function you thought you were calling!

Filed under: Javascript, Programming — Michi @ 11:32 am

Share this

  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • description
  • Technorati
  • Slashdot
  • co.mments
  • NewsVine

Related

Since my dear reader Sameer requested it, I'm here making an update. I've got a cool JavaScript fix for everybody! I mentioned in a post a long time ago, but JavaScript has this semi-unexpected "feature" where you can accidentally overwrite...
Several months ago, my friend informed me that he was seeing a large volume of email spam coming from one of my legacy sites. After investigating, we found that my "contact" page was the source. A hacker was spamming people...

2 Comments »

TrackBack URI | Blog RSS | Comment RSS

  1. Well it’s stupid that you are allowed to overwrite a method with a field.

    Comment by Andrei — November 8, 2006 @ 10:15 am

  2. Sorry for commenting on such old blog post but there is a typo in your code.

    document.form['myForm'].submit(); should be document.forms['myForm'].submit();

    Other than that, nice article. ;)

    Comment by Sanjay — August 9, 2007 @ 8:02 pm

What do you think?