Beware of form parameters named ’submit’

Well, that is if you are ever going to be submitting the form via Javascript. We had to generate forms on the fly and POST them behind the scenes (i.e. in hidden <iframe>s), and got a decent script going, until a particular case failed for no apparent reason. I finally found that the problem was due to a form parameter named “submit” that overwrote the submit() function of the <form>. That’s, of course, after looking at all the wrong places.

So this doesn’t work:

<form id="ninjaForm" action="/come/get/some" method="post">
  <input id="someParam" name="someParam"
    type="hidden" value="Some value" />
  <input id="submit" name="submit"
    type="hidden" value="Start search" />
</form>
<script type="text/javascript">
  setTimeout("document.ninjaForm.submit()", 200);
</script>

When the browser tries to execute “document.ninjaForm.submit()”, it sees the “submit” form field (which overwrote the submit() function) instead and complains that “submit is not a function”.

Do this instead:

<form id="ninjaForm" action="/come/get/some" method="post">
  <script type="text/javascript">
    // Alias the submit function in case there is a 'submit' param.
    ninjaForm = document.getElementById('ninjaForm');
    ninjaForm.__submit = ninjaForm.submit;
  </script>
  <input id="someParam" name="someParam"
    type="hidden" value="Some value" />
  <input id="submit" name="submit"
    type="hidden" value="Start search" />
</form>
<script type="text/javascript">
  setTimeout("ninjaForm.__submit();", 200);
</script>

That’s one way to workaround, of course, and we can get away with the “ninjaForm” global in this case.

3 Comments & TrackBacks ()

Paper doll icon
littletooter's Gravatar

You saved my life, I did the same mistake & wondering what’s wrong for more than 3 hours until now.

Posted by: littletooter on June 9, 2007 5pm

Paper doll icon
Devin Martin's Gravatar

Thank you for posting this. It took me 4 hours to figure this stupid little bug out. I think my boss was getting annoyed with me.

Posted by: Devin Martin on October 11, 2007 2am

Paper doll icon
Mak's Gravatar

thank yoU!

Posted by: Mak on May 7, 2008 10am

You can subscribe to the RSS feed for comments on this post.

Sorry, this entry is no longer accepting comments. If you have something you really want to say, you can write me.