php 表单上的多个按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4120678/
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-25 12:02:48  来源:igfitidea点击:

multiple buttons on a form

phphtml

提问by acctman

I have a clear button that I want to tie into some php coding. how do I detect if the clear button is pressed. When the user press clear, i'm going to have it update a sql table to clear out entries. but first I need to know when the button is pressed.

我有一个清晰的按钮,我想将其绑定到一些 php 编码中。如何检测是否按下了清除按钮。当用户按清除时,我将更新 sql 表以清除条目。但首先我需要知道按钮何时被按下。

<input name="Reset1" type="reset" value="clear" />

回答by Ben

You check the postor getdata from your form, using the name of the button:

您可以使用按钮的名称检查表单中的postget数据:

<form action='' method='post'>
  <button type='submit' name='reset'>Clear</button>
  <button type='submit' name='submit'>Submit</button>
</form>

PHP (after submission):

PHP(提交后):

if(isset($_POST['reset'])) { /* ...clear and reset stuff... */ }
else if(isset($_POST['submit']) { /* ...submit stuff... */ }

Alternatively, you have two buttons with the same name, which both submit your form, and if/elsetheir values:

或者,您有两个具有相同名称的按钮,它们都提交您的表单if/else及其值:

<form action='' method='post'>
  <button name='submit' value='0'>Clear</button>
  <button name='submit' value='1'>Submit</button>
  <button name='submit' value='2'>Something Else</button>
</form>

PHP (after submission):

PHP(提交后):

if($_POST['submit']==0)      { /* ...clear and reset stuff... */ }
else if($_POST['submit']==1) { /* ...submit stuff... */ }
else if($_POST['submit']==2) { /* ...do something else... */ }

回答by Zuorion

Solution from: http://websistent.com/multiple-submit-buttons-in-php/

解决方案来自:http: //websistent.com/multiple-submit-buttons-in-php/

html:

html:

<input type="submit" name="btn_submit" value="Button 1" />
<input type="submit" name="btn_submit" value="Button 2" />
<input type="submit" name="btn_submit" value="Button 3" />

php:

php:

<?php
if($_REQUEST['btn_submit']=="Button 1")
{
print "You pressed Button 1";
}
else if($_REQUEST['btn_submit']=="Button 2")
{
print "You pressed Button 2";
}
else if($_REQUEST['btn_submit']=="Button 3")
{
print "You pressed Button 3";
}
?>

回答by user1324491

<form action="" method="post" >

    <input type="submit" name="btn_submit" value="Button 1" />
    <input type="submit" name="btn_submit" value="Button 2" />
    <input type="submit" name="btn_submit" value="Button 3" />
<form>

The variables in $_REQUEST are provided to the script via the GET, POST, and COOKIE ,you can use $_POST instead as the form's method is POST

$_REQUEST 中的变量通过 GET、POST 和 COOKIE 提供给脚本,您可以使用 $_POST 代替,因为表单的方法是 POST

<?php

    if (isset($_REQUEST['btn_submit'])) {

        switch ($_REQUEST['btn_submit']) {
           case "Button 1":
                 echo "You pressed Button 1";
                 break;
           case "Button 2":
                 print "You pressed Button 2";
                 break;
           case "Button 3":
                 print "You pressed Button 3";
                 break;
        }
    }
?>

回答by Billy ONeal

Either wire up the item as a button you attach to a Javascript snippet that fires off an AJAX request, or use a submit input. (Determine whether the form was submitted or reset based upon the submit value)

将项目作为按钮连接到触发 AJAX 请求的 Javascript 片段,或使用提交输入。(根据提交值判断表单是提交还是重置)

回答by bgcode

You can bind an ajax event to it being clicked. If you're using jQuery, you can bind the ajax()function to its click event. The ajax function allows for easy passing of variables, error checking, etc.

您可以将 ajax 事件绑定到被单击的事件。如果您使用 jQuery,您可以将ajax()函数绑定到它的 click 事件。ajax 函数允许轻松传递变量、错误检查等。

You'll ajax call a php file, let's call it reset_ajax.php, which will make the query.

您将 ajax 调用一个 php 文件,我们称其为 reset_ajax.php,它将进行查询。

The other option is to keep track of the reset button being clicked with a hidden input that's updated every time its clicked, and then you can make the query on the backend upon form submission.

另一种选择是跟踪被点击的重置按钮,每次点击时都会更新隐藏的输入,然后您可以在提交表单时在后端进行查询。

Up to you which method you want to use. The first one has the advantage of working even if they don't submit the form.

由您决定要使用哪种方法。即使他们不提交表单,第一个也有工作的优势。

回答by edwin

surprisingly, the formactionattribute was not covered. you can use it like so

令人惊讶的是,该formaction属性没有被覆盖。你可以像这样使用它

<form method="post" action="/action/project/edit">
    ...
    <button type="submit">edit</button>
    <button type="submit" formaction="/action/project/delete">delete</button>
</form>

of course, this does not use javascript and has good browser support. this route (no pun intended) has the added benefit of not requiring extra if / then statements on the server since the same route won't be used for two different things

当然,这不使用javascript并且具有良好的浏览器支持。这条路线(没有双关语)的额外好处是不需要服务器上额外的 if / then 语句,因为同一路线不会用于两种不同的事情

reference https://css-tricks.com/separate-form-submit-buttons-go-different-urls/

参考 https://css-tricks.com/separate-form-submit-buttons-go-different-urls/