¿Qué pasa con el botón de submit si envío un form HTML con la tecla Intro?

Nos enfrentamos a un bug ciertamente extraño. Tenemos dos forms en los que hacemos lo mismo: hay un <input type=»submit» name=»button_send» value=»Aceptar»>, y en el script que recibe el form estamos preguntando en los datos de GET o POST si viene un campo llamado «button_send».

En Firefox y otros no hay ningún problema… pero al llegar a Explorer comienzan (qué raro) los comportamientos extraños. En uno de los forms, al pulsar Intro mientras el foco está en uno de los campos que no es el botón, el formulario se envía pero el «button_send» NO APARECE en los datos recibidos. Lo impresionante del asunto es que en el otro form sí que aparece incluso pulsando Intro, y ambos forms tienen la misma estructura, sólo se diferencian en los campos de datos que contienen.

Tras un buen rato de comernos el coco y flipar bastante, llegamos a la solución leyendo este texto (hay que ir a archive.org porque el original se borró):

http://web.archive.org/web/20060518010241/ppewww.ph.gla.ac.uk/~flavell/www/formquestion.html

There seems to be some confusion/disagreement among browser developers as to whether a Submit control is a «successful control» (in the terms of e.g the HTML4 specification) when a form is submitted by means of Enter.

MSIE4’s behaviour seemed odd. If there was only a single text-input field, then hitting Enter with the focus on the text-input field would submit the form without sending the name/value pair associated with a submit control. Surprisingly, however, if there were two text-input fields, then hitting Enter when the focus was on either of those fields would result in the form being submitted together with the name/value pair of the submit control (or of the first submit control if there were more than one). This seemed illogical and inconsistent.

Básicamente, el comportamiento de Explorer (comenzando por IE4 pero se ha mantenido en los siguientes, al menos hasta el 8 ) es que si el form tiene exactamente 1 <input type=»text»>, entonces el botón de submit no se envía; pero si tiene más de uno, sí.

El resto de navegadores se comportan de maneras variadas ante este hecho, aunque ninguna tan barroca como esta. La causa es que el comportamiento de enviar un form con Intro no está modelado en la especificación del W3C.

¿Solución?

Si nuestro form tiene varios botones, el script debe contemplar un caso por defecto para cuando no se reciba ninguno.

Si el form es modo GET y estábamos leyendo el botón para distinguir si el usuario ha enviado el formulario o sólo leído la página que lo contiene, hay que cambiar de método, es mejor usar un <input type=»hidden»> y leer éste en lugar del botón.

Deja un comentario