More on submitting forms

November 28th, 2005

At the heart of every dynamic website is a way for the user to interact with the data it provides, and one very common way of interacting with with such data is by the use of HTML Forms. Generally (and hopefully obviously), a form is "submitted" when the user activates an HTML input element with the type attribute set to submit. However, most user agents will also submit a form when you give focus to an input element with attribute set to text and hit return. With PHP, the most common way to test if a form has been submitted is to simply test if the $_POST/$_GET array is empty or not, however when using the aforementioned multiple submit button techniques, a problem arises:

  • sensible user agents (Gecko based browser, for example) will simply act as though the first submit button had been pressed, and proceed normally.
  • MSIE (Internet Exploder?) will submit the form without passing any submit elements in the resulting array, which can cause all kinds of problems if you want to test their validity

The Solution? It is perfectly valid to have multiple input elements with the same name attribute, so simply place a hidden field with the same name/value as your preferred default action. For example:

<input type="hidden" name="action" value="Edit" />
<input type="submit" name="action" value="Edit" />
<input type="submit" name="action" value="Delete" />

This way, the field called action with value Edit is always passed, unless the user chooses otherwise.

Note: It is very important that the hidden field appears before the buttons themselves, otherwise the action field will always be set to Edit!


 Add to del.icio.us    Digg this    Technorati

Related Posts:

Entry Filed under: PHP, MySQL, Markup

1 Comment Add your own

  • 1. civ  |  December 6th, 2005 at 12:35 pm

    It is indeed a shame that only IE manages to get it right, and not decide for the user which button they should have pressed.

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

November 2005
M T W T F S S
    Dec »
 123456
78910111213
14151617181920
21222324252627
282930  

Most Recent Posts