hitting enter on a form instead of clicking the button

P

Paul Furman

I'm getting incorrect response when hitting the enter key instead of
actually clicking the button on a form. It activates but the post data
[value='Update'] isn't being sent I think. The php generated page
flickers & reloads but doesn't perform the update.

This is php generated, I'm just pasting the page source (reformatted &
extra junk removed and I think they are now identical), and it's running
on my localhost apache server for testing so maybe there's something
funny with my setup. Crossposted to html & php groups.


Must click button (no enter):

<form action='?SC=cart.php' method='POST'>
<tr>
<td>
<input type='hidden' name='item' value='6077'>name
</td>
<td>
<input type='submit' name='buy' value='Update'>
</td>
<td>
<input type='text' name='item_qty' size='1' value='2'>
</td>
</tr>
</form>


Enter or click works fine:

<form action='?SC=cart.php' method='POST'>
<tr>
<td>
<input type='hidden' name='item' value='6540'>name
</td>
<td>
<input type='submit' name='buy' value='Update'>
</td>
<td>
<input type='text' name='item_qty' size='1' value='2'>
</td>
</tr>
</form>



Here they are without me fixing up the formatting:

Must click button (no enter):

<form action='?SC=cart.php' method='POST'><tr>
<td><input type='hidden' name='item' value='6540'>Adiantum jordanii / -
1 gal</td>
<td><input type='submit' name='buy' value='Update'></td>
<td><input type='text' name='item_qty' size='1' value='1'></td>
<td>$&nbsp;9.00 </td><td>$&nbsp;9.00 </td><td>0%</td>
<td>10%</td>
</tr></form>


Enter or click works fine:

<form action='?SC=cart.php' method='POST'><tr>
<td><input type='hidden' name='item' value='6077'>Adiantum jordanii / -
1 gal</td>
<td><input type='submit' name='buy' value='Update'></td>
<td><input type='text' name='item_qty' size='1' value='2'></td>
<td>$&nbsp;10.00 </td><td>$&nbsp;20.00 </td></tr></form>
<tr><th colspan='4' align='right'>Tax:</td><td>$
1.70</td><td>&nbsp;</td></tr>
<tr><th colspan='4' align='right'>Delivery:</td><td>$
0.00</td><td>&nbsp;</td></tr>
<tr><th colspan='4' align='right'>Order Total:</th><th>$&nbsp;21.70</th>
<th><form action='?SC=checkout.php' method='post'><input type='submit'
name='checkout' value='Checkout'></form>
 
?

=?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=

Paul said:
I'm getting incorrect response when hitting the enter key instead of
actually clicking the button on a form. It activates but the post data
[value='Update'] isn't being sent I think. The php generated page
flickers & reloads but doesn't perform the update.

That's the way it works - The name/value pair of a submit button only gets
submitted if the button is actualy pressed.

If you want to have some data every time a form is submitted, rely on <input
type='hidden'/> fields.

(Check the W3.org HTML reference for more details).

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

In Marseilles they make half the toilet soap we consume in America, but
the Marseillaise only have a vague theoretical idea of its use, which they
have obtained from books of travel.
-- Mark Twain
 
P

Paul Furman

Iván Sánchez Ortega said:
Paul Furman wrote:

I'm getting incorrect response when hitting the enter key instead of
actually clicking the button on a form. It activates but the post data
[value='Update'] isn't being sent I think. The php generated page
flickers & reloads but doesn't perform the update.


That's the way it works - The name/value pair of a submit button only gets
submitted if the button is actualy pressed.

If you want to have some data every time a form is submitted, rely on <input
type='hidden'/> fields.

(Check the W3.org HTML reference for more details).

I'm not sure why it worked before in the one case but THANKS!

I added:
<input type='hidden' name='buy' value='Update'>

on top of the old:
<input type='submit' name='buy' value='whatever'>

and it works!

The test is simply:
if (isset($_REQUEST["buy"])) {
 
J

Jukka K. Korpela

Scripsit Paul Furman:
I'm getting incorrect response when hitting the enter key instead of
actually clicking the button on a form.

Define "incorrect". The snippet of HTML that you posted is syntactically
malformed. There is no specification that says what should happen then.
It activates but the post data
[value='Update'] isn't being sent I think.

It probably isn't. This has been left to browsers, but they generally don't
treat a submit button as "successful" if it isn't used, which is rather
reasonable if you ask me.
This is php generated, I'm just pasting the page source (reformatted &
extra junk removed and I think they are now identical), and it's
running on my localhost apache server for testing so maybe there's
something funny with my setup.

As usual, you should have constructed a static HTML page that demonstrates
the problem and posted its URL.
Crossposted to html & php groups.

As usual, crossposting resulted from insufficient thinking. There is really
no reason to suspect that PHP is involved in the submission problem. You
would have seen this if you had constructed the static page. (Followups
trimmed.)
Must click button (no enter):
Huh?

<form action='?SC=cart.php' method='POST'>
<tr>
<td>
<input type='hidden' name='item' value='6077'>name
</td>
<td>
<input type='submit' name='buy' value='Update'>
</td>
<td>
<input type='text' name='item_qty' size='1' value='2'>
</td>
</tr>
</form>

This is syntactically malformed: you cannot have a <tr> element as a child
of <form>.

And why do you say "no enter"? I can submit it by pressing enter if I want.

So what is your question? It's probably something closely related to some
questions in the FAQ, which you apparently didn't check before posting.

If you want to have a field always appearing in the form data (why?), then
it should be included as a hidden field _only_, preferably _without_ another
field with the same name in the same form, since it sounds like your form
handler is simplistic and could get confused with that.
 
P

Paul Furman

Jukka said:
Scripsit Paul Furman:
It activates but the post data
[value='Update'] isn't being sent I think.

It probably isn't. This has been left to browsers, but they generally
don't treat a submit button as "successful" if it isn't used, which is
rather reasonable if you ask me.

But below you say using the enter key should work, that was the problem,
it doesn't work.

As usual, you should have constructed a static HTML page that
demonstrates the problem and posted its URL.

I think it was pretty damned simple.

As usual, crossposting resulted from insufficient thinking. There is
really no reason to suspect that PHP is involved in the submission
problem. You would have seen this if you had constructed the static
page. (Followups trimmed.)

Who knows? Mysterious things happen. I didn't know what the problem was.

This is syntactically malformed: you cannot have a <tr> element as a
child of <form>.

Obviously this is a snippet and there is a <table> structure above this
snippet.

And why do you say "no enter"? I can submit it by pressing enter if I want.

No, that was the problem, it doesn't pass:
if (isset($_REQUEST["buy"]))
when using the enter key, only when clicking the button.
I should have included the if clause in my OP.

So what is your question? It's probably something closely related to
some questions in the FAQ, which you apparently didn't check before
posting.

What FAQ?

If you want to have a field always appearing in the form data (why?),
then it should be included as a hidden field _only_, preferably
_without_ another field with the same name in the same form, since it
sounds like your form handler is simplistic and could get confused with
that.

Yes, this was the problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top