PHP 如何确定用户是否按下了 Enter 键或提交按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/795132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:52:30  来源:igfitidea点击:

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

phpformssubmitform-submit

提问by Mike Dinescu

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.

每当用户按下 时Enter,就好像最上面的提交输入正在被按下,因此它会导致代码检查哪个输入被点击时出现问题。

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 Enterkey?

PHP 有没有办法确定输入是否被点击,或者只是用户按下Enter键时选择的输入?

回答by Peter Bailey

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

只要您正确构建 HTML,您就可以确定使用了哪个按钮

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

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

$_POST阵列(或$_GET/$_REQUEST)将包含键“动作”与颁布按钮的值(是否点击或没有)。

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>

回答by Mike Dinescu

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

PHP 是服务器端技术,因此在服务器上运行 - 因此它无法确定在客户端(也就是用户)上按下了哪些键。也就是说,当然,除非您专门对客户端进行编码以在服务器请求中包含此类信息(发布表单数据也是一种请求形式)。

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

实现这一目标的一种方法是在您的客户端代码中使用 Javascript。

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

请参阅此页面作为有关使用 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/domeventsoffers 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 :)

您可能还需要为页面上的按键事件添加一个侦听器,以便捕获用户按下 Enter 键并记录此信息。 http://www.howtocreate.co.uk/tutorials/javascript/domevents提供了关于在 Javascript 中添加/删除事件侦听器的主题的讨论,但在使用事件时必须非常小心,因为使用不当它们可能成为内存泄漏难以调试并导致用户不满意:)

回答by EvanK

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.

PHP 本身无法确定表单提交事件是如何触发的,因为这发生在客户端,而 PHP 是一种服务器端语言。您必须实现 Javascript 来侦听 -- 并记录到服务器端 -- 按键和鼠标点击,然后分析该数据以找到您要查找的内容。

Now, PHP can tell whichsubmit 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 Enterkey press). You could re-order all your submits so as to control which submit is triggered.

现在,PHP 可以判断触发了哪个提交输入,因为它会出现在表单数据中,而其他则不会。大多数浏览器将第一个提交输入设为默认输入(按下Enter键时触发的输入)。您可以重新排序所有提交以控制触发哪个提交。

回答by marcgg

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

PHP 无法真正知道客户端发生了什么。

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.

我建议使用javascript。当用户执行操作时,捕获它并将其存储在将与表单一起提交的隐藏字段中。您还可以跟踪哪些输入处于活动状态并将其存储在隐藏字段中。

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

希望有帮助

回答by Andy Baird

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

这就是您在客户端编写标记的方式。

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

例如,这是您可以执行此操作的一种(非 XHTML)方式:

In the HTML file:

在 HTML 文件中:

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

在 myform.php 中:

if ($_POST['pressed_button']=='false') {
   // Logic for enter key
} else {
   // Logic for button press
}