hitting enter on a form instead of clicking the button

Discussion in 'HTML' started by Paul Furman, Mar 15, 2007.

  1. Paul Furman

    Paul Furman Guest

    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>
     
    Paul Furman, Mar 15, 2007
    #1
    1. Advertising

  2. 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).

    --
    ----------------------------------
    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
     
    =?ISO-8859-15?Q?Iv=E1n_S=E1nchez_Ortega?=, Mar 15, 2007
    #2
    1. Advertising

  3. Paul Furman

    Paul Furman Guest

    Iván Sánchez Ortega wrote:
    > 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"])) {
     
    Paul Furman, Mar 15, 2007
    #3
  4. 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.

    --
    Jukka K. Korpela ("Yucca")
    http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Mar 16, 2007
    #4
  5. Paul Furman

    Paul Furman Guest

    Jukka K. Korpela wrote:

    > 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.
    >
    >> Crossposted to html & php groups.


    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.


    >> <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>.


    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.
     
    Paul Furman, Mar 17, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. 23s
    Replies:
    2
    Views:
    3,982
  2. TS
    Replies:
    5
    Views:
    14,603
    Jeffrey Tan[MSFT]
    Nov 29, 2005
  3. Replies:
    4
    Views:
    3,812
  4. Sunfire
    Replies:
    2
    Views:
    1,086
    Sunfire
    Nov 6, 2007
  5. Guest
    Replies:
    2
    Views:
    1,670
    Guest
    Oct 25, 2004
Loading...

Share This Page