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

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

孤檠 发布于 2021-11-28 字数 532 浏览 685 回复 5 原文

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?

如果你对这篇文章有疑问,欢迎到本站 社区 发帖提问或使用手Q扫描下方二维码加群参与讨论,获取更多帮助。

扫码加入群聊

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

尘曦 2022-06-07 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
}
半边脸i 2022-06-07 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

多彩岁月 2022-06-07 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.

魄砕の薆 2022-06-07 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 :)

岁月打碎记忆 2022-06-07 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>