How can PHP determine if the user pressed the Enter key or Submit button?

30,518

Solution 1

You can identify which button was used provided you structure your HTML correctly

<input type="submit" name="action" value="Edit">
<input type="submit" name="action" value="Preview">
<input type="submit" name="action" value="Post">

The $_POST array (or $_GET/$_REQUEST) will contain the key "action" with the value of the enacted button (whether clicked or not).

Now, "clicking" is explicitly a client-side behavior - if you want to differentiate between a click and a keypress, you'll need to add some scripting to your form to aid in that determination.

Edit

Alternatively, you can be "sneaky" and use a hidden submit that should correctly identify a key-pressed for submission, but this probably has some significant impact on accessibility.

<?php

if ( 'POST' == $_SERVER['REQUEST_METHOD'] )
{
    echo '<pre>';
    print_r( $_POST );
    echo '</pre>';
}

?>
<form method="post">

    <input type="text" name="test" value="Hello World">

    <input type="submit" name="action" value="None" style="display: none">
    <input type="submit" name="action" value="Edit">
    <input type="submit" name="action" value="Preview">
    <input type="submit" name="action" value="Post">

</form>

Solution 2

Roberto,

PHP is server-side technology and therefore runs on the server - so there is no way for it to determine what keys where pressed at the client (aka the user). That is, of course, unless you specifically code the client-side to include such information with the server requests (posting form data is a form of request too).

One way to accomplish that is by using Javascript in your client code.

See this page as a starting point regarding handling form submit events using Javascript. http://www.w3schools.com/jsref/jsref_onSubmit.asp

You may also have to add a listener for key press events on your page in order to capture the user pressing the Enter key and then recording this information. http://www.howtocreate.co.uk/tutorials/javascript/domevents offers a discussion on the topic of adding/removing event listeners in Javascript but you have to be very careful when using events because improperly used they can be the source of memory leaks which are hard to debug and cause for unhappy users :)

Solution 3

PHP alone can't determine how the form submit event was triggered, because that happens on the client-side while PHP is a server-side language. You'd have to implement Javascript to listen for -- and log to the server-side -- key presses and mouse clicks, and then analyze that data to find what you're looking for.

Now, PHP can tell which submit input was triggered, as it will appear in the form data while the others will not. Most browsers make the first submit input the default (the one that is triggered on an Enter key press). You could re-order all your submits so as to control which submit is triggered.

Solution 4

PHP can't really know what happened on the client side.

I'd recommend using javascript. When the user do the action, catch it and store it in an hidden field that will be submited with the form. You can also keep track of what input is active and store it in an hidden field.

The code would go a bit like that (i didnt checked the syntax)

<input type="text" onfocus="setCurrent(this)" id="1" />
<input type="hidden" id="hid" />

function setCurrent(o){
  $('hid').value = o.id;
}

I think that playing around with events catching and hidden fields should give you the result that you want.

Hope that helps

Solution 5

It's how you write the markup on the client side.

For example, here is one (non-XHTML) way you could do this:

In the HTML file:

<form method="post" action="myform.php" id="myform">
  ... form items here ...

<input type="submit" name="enter_key" value="true" style="display:none">
<input type="hidden" name="pressed_button" id="pressed_button" value="false">
<input type="button" value="Submit"
  onclick="document.getElementById('pressed_button').value='true';document.getElementById('myform').submit();">
</form>

In myform.php:

if ($_POST['pressed_button']=='false') {
   // Logic for enter key
} else {
   // Logic for button press
}
Share:
30,518
Peter Mortensen
Author by

Peter Mortensen

Experienced application developer. Software Engineer. M.Sc.E.E. C++ (10 years), software engineering, .NET/C#/VB.NET (12 years), usability testing, Perl, scientific computing, Python, Windows/Macintosh/Linux, Z80 assembly, CAN bus/CANopen. Contact I can be contacted through this reCAPTCHA (requires JavaScript to be allowed from google.com and possibly other(s)). Make sure to make the subject specific (I said: specific. Repeat: specific subject required). I can not stress this enough - 90% of you can not compose a specific subject, but instead use some generic subject. Use a specific subject, damn it! You still don't get it. It can't be that difficult to provide a specific subject to an email instead of a generic one. For example, including meta content like "quick question" is unhelpful. Concentrate on the actual subject. Did I say specific? I think I did. Let me repeat it just in case: use a specific subject in your email (otherwise it will no be opened at all). Selected questions, etc.: End-of-line identifier in VB.NET? How can I determine if a .NET assembly was built for x86 or x64? C++ reference - sample memmove The difference between + and &amp; for joining strings in VB.NET Some of my other accounts: Careers. [/]. Super User (SU). [/]. Other My 15 minutes of fame on Super User My 15 minutes of fame in Denmark Blog. Sample: Jump the shark. LinkedIn @PeterMortensen (Twitter) Quora GitHub Full jump page (Last updated 2021-11-25)

Updated on July 15, 2020

Comments

  • Peter Mortensen
    Peter Mortensen almost 4 years

    The problem I have is that I have multiple submit inputs in a single form. Each of these submit inputs has a different value and I would prefer to keep them as submit.

    Whenever the user presses Enter, it is as though the topmost submit input is being pressed, and so it is causing problems for the code checking which input was clicked.

    Is there a way for PHP to determine whether or not the input was clicked, or was just the input that was selected when the user pressed the Enter key?

  • Admin
    Admin about 15 years
    I guess I assumed the browser would pass the information on to PHP concerning how the form was submitted. Your solution was one I was considering, and now seems the only solution.
  • Admin
    Admin about 15 years
    This only works when you click on the button. It doesn't work when you press Enter in one of the text inputs.
  • Peter Bailey
    Peter Bailey about 15 years
    Um... yes it does. The first one by source-order will be "chosen" by the browser as the submit actor.
  • Admin
    Admin about 15 years
    Yes chosen by the browser, not the user.
  • Admin
    Admin about 15 years
    How would the hidden input impact accessibility? I already have a default behavior that (I would hope) executes when the user presses the enter key, so if that's what you meant, I have it covered.
  • Robert K
    Robert K about 15 years
    Roberto, the first submit-button in the source is chosen when enter is pressed. Thus the user chooses it by pressing enter whether they know it or not.
  • Admin
    Admin about 15 years
    Ok great. This is the solution I wanted. Thanks.