【问题标题】:More than one submit button多个提交按钮
【发布时间】:2013-04-16 06:59:25
【问题描述】:

我在使用 HTML 和 PHP 中的多个提交按钮时遇到问题,我尝试为基于 Web 的计算器编写 GUI 代码。这真的很容易,但是php中的函数并不那么容易。所以我有这个带有 6 个提交按钮的简单 GUI:

<?php 
$output= isset($_POST['output']) ? $_POST['output'] : "";

function calc(){
//calculate...
}

?>
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="utf-8" />
<title>Calc</title>
<style type="text/css">
 .bwidth {
      width: 100%;
}
</style>
</head>
<body>
    <form action="calc.php">
        <h1>
            <u>MY Own Calculator</u>
        </h1>
        <table border="3px" type="box" cellspacing="5px" width="250px">
            <tr style="height: 24px;">
                <td colspan="3"><input type="text" name="<?php $ausgabe ?>"
                    value="0" style="width: 98%;" /></td>
            </tr>
            <tr>
                <td><input class="bwidth" type="submit" name="1" value="1" /></td>
                <td><input class="bwidth" type="submit" name="2" value="2" /></td>
                <td><input class="bwidth" type="submit" name="3" value="3" /></td>

            </tr>
            <tr>
                <td><input class="bwidth" type="submit" name="minus" value="-" /></td>
                <td><input class="bwidth" type="submit" name="plus" value="+" /></td>
                <td><input class="bwidth" type="submit" name="enter"
                    value="=" /></td>
            </tr>
        </table>
    </form>
</body>

现在我如何区分这些提交按钮?甚至可以有多个提交按钮?我尝试按值分隔按钮...但没有用。

并且某人知道如何将值添加到文本字段中的现有值?所以我可以按下按钮 1,它会在文本字段中写入 1,当我按下按钮 2 时,它会添加数字 2,所以它会变成“12”?

感谢所有建议!

【问题讨论】:

  • 我认为你真正需要的是几个普通的按钮和一个javascript方法。
  • 是的,我赞成。这个不用php写,把所有的按钮都改成type="button",用jquery/javascript写支持按钮的函数。此外,为所有表单元素添加 id,以便您可以使用 id 作为引用从 javascript 调用它们。
  • 问题是我必须用php写,学校需要。
  • 是否可以给每个提交按钮一个 ID?

标签: php button submit


【解决方案1】:

在你的 php 中

if(isset($_POST['minus'])) {
  // selected minus
}

if(isset($_POST['plus'])) {
  // selected plus
}

if(isset($_POST['enter'])) {
  // selected enter
}

当您单击一个按钮时,它将与您的帖子数据一起发送到服务器。您在 $_POST 数组中找到的所有发布数据。

如果您想查看 $_POST 数组中的内容,可以运行以下小代码:

<?php
   echo '<pre>' . print_r($_POST, true) . '</pre>';
?>

【讨论】:

    【解决方案2】:

    您可以尝试使用isset($_GET['input_name']) 验证设置了哪个按钮。

    或者您可以命名不同的输入:

    <form action="calc.php">
        <h1>
            <u>MY Own Calculator</u>
        </h1>
        <table border="3px" type="box" cellspacing="5px" width="250px">
            <tr style="height: 24px;">
                <td colspan="3"><input type="text" name="<?php $ausgabe ?>"
                    value="0" style="width: 98%;" /></td>
            </tr>
            <tr>
                <td><input class="bwidth" type="submit" name="digit" value="1" /></td>
                <td><input class="bwidth" type="submit" name="digit" value="2" /></td>
                <td><input class="bwidth" type="submit" name="digit" value="3" /></td>
    
            </tr>
            <tr>
                <td><input class="bwidth" type="submit" name="operation" value="-" /></td>
                <td><input class="bwidth" type="submit" name="operation" value="+" /></td>
                <td><input class="bwidth" type="submit" name="operation" value="=" /></td>
            </tr>
        </table>
    </form>
    

    在你的函数中:

    function calc(){
        $op = isset($_GET['operation']) ? $_GET['operation'] : null;
        switch ($op) {
           case "+": .... ; break;
           case "-": .... ; break;
           case "=": .... ; break;
           default: "not supported";
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 JavaScript 执行此操作,首先您必须将您的 &lt;form&gt; 更改为:

      <form id="form1" name="form1" method="post" action="calc.php">
      

      那么 JavaScript 和按钮应该如下所示:

      <script>
      function submitForm(action)
      {
          document.getElementById('form1').action = action;
          document.getElementById('form1').submit();
      }
      </script>
      
      ...
      
      <input type="button" onclick="submitForm('page1.php')" value="submit 1" />
      <input type="button" onclick="submitForm('page2.php')" value="submit 2" />
      

      【讨论】:

        【解决方案4】:

        也许您可以为所有提交按钮使用一个名称。

        <input type="submit" name="operator" value="+" />
        <input type="submit" name="operator" value="-" />
        <input type="submit" name="operator" value="=" />
        

        你可以在php中获取值

        <?php
            $opr = $_GET['operator'];
            if($opr == "+") {
                 //do something
            } else if($opr == "-") {
                 //do something
            } else if($opr == "=") {
                 //do something
            }
        ?>
        

        【讨论】:

        • @Synoon - 你能分享一下你尝试过的东西吗?也请您在表单标签中添加“method='get'”后再试一次。
        【解决方案5】:

        当您向calc.php提交表单时

        calc.php 内部检查接收到的表单元素。

        if(isset($_REQUEST['minus']))
        {
            // minus is clicked
        }
        else if(isset($_REQUEST['plus']))
        {
            // plus is clicked
        }
        else if(isset($_REQUEST['enter']))
        {
            // enter is clicked
        }
        

        单击的按钮将与表单一起发送,而不是其他按钮...

        希望对你有帮助...

        【讨论】:

        • 这并不总是正确的。我看到一些情况,当提交元素名称未传递给服务器时。
        • @MartinPerry - 哪些情况?
        • 有时,当我用 Enter 确认表单时。我想我在旧版 IE 中看到了这种行为。
        • @MartinPerry - 好的 :-) 旧版本的 IE 并不总是能跟上标准。新的也不是:-)
        【解决方案6】:

        是的,一个表单中可以有 n 个提交按钮。

        在与提交按钮相同的行中指定操作页面。

        <form action="actionpage.php">
        <input type="submit" value="1">
        <input type="submit" value="2" formaction="postPage.php">
        </form>
        

        已更新 - 如果 IE 出现问题

        <form>
        <input type="submit" value="1" onclick="document.formName.action='abc.php';">
        <input type="submit" value="2" onclick="document.formName.action='xyz.php';">
        </form>
        

        【讨论】:

        • 如果这能工作会很酷... ...在所有浏览器中。它仅适用于 >=IE 10、Firefox、Opera、Chrome 和 Safari。当然,这取决于目标用户。
        • 这应该适用于所有浏览器,我很久以前就工作过。从来没有收到客户的负面反馈哈哈哈......所以尝试一次,因为它是输入标签的属性。
        • 这是一个 HTML5 属性,所以它在早期版本的 IE 中不起作用。非负面反馈并不总是与“随处工作”一回事。看看:nostrongbeliefs.com/form-with-two-submit-buttons-using-html5
        • hmmm....那么避免 IE 问题的最佳方法是使用 thie onclick="document.formName.action='actionPage.php';"
        • 是的,这是一个选择。
        【解决方案7】:

        您可以使用 JavaScript 提交表单。点击按钮后,调用 JS 函数,通过推送操作设置一些隐藏元素,然后提交表单到服务器。

        【讨论】:

          猜你喜欢
          • 2014-03-03
          • 1970-01-01
          • 2016-06-22
          • 1970-01-01
          • 1970-01-01
          • 2012-01-23
          • 2017-04-12
          • 1970-01-01
          相关资源
          最近更新 更多