php 多个提交按钮php不同的动作

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

Multiple submit buttons php different actions

phpsubmitaction

提问by Joegramming

I have a website started where I want to have 2 separate submit buttons, one of which will take data entered and do some calculations to it to display on the same screen. I've got this successfully working with:

我有一个网站,我想在其中设置 2 个单独的提交按钮,其中一个按钮将获取输入的数据并对其进行一些计算以显示在同一屏幕上。我已经成功地使用了这个:

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php">
<input type="submit" name="calc" value="Find Angle">

and then I use:

然后我使用:

if (!isset($_POST['submit'])){
Do actions, display calculations}

Now I want a second submit button that still grabs the data they entered but then goes to a different address. Is there an elegant way to do this?

现在我想要第二个提交按钮,该按钮仍会抓取他们输入的数据,但随后会转到不同的地址。有没有一种优雅的方法来做到这一点?

回答by Travesty3

You could add an onclickmethod to the new submit button that will change the action of the form and then submit it.

您可以onclick向新的提交按钮添加一个方法,该方法将更改表单的操作,然后提交它。

<script type="text/javascript">
  function submitForm(action) {
    var form = document.getElementById('form1');
    form.action = action;
    form.submit();
  }
</script>

...

<form id="form1">
  <!-- ... -->
  <input type="button" onclick="submitForm('page1.php')" value="submit 1" />
  <input type="button" onclick="submitForm('page2.php')" value="submit 2" />
</form>

回答by Pradeep

You can change the form action by using formaction="page1.php" in button property .

您可以通过在 button 属性中使用 formaction="page1.php" 来更改表单操作。

<form id="form1" name="form1" method="post" onsubmit="" onreset="" action="programname.php">
    <input type="submit" name="calc" value="Find Angle">

    <input type="button" type="submit" formaction="page1.php">Action 0</button>
    <input type="button" type="submit" formaction="page2.php">Action 1</button>
</form>

Note: The formaction attribute of the button tag is not supported in Internet Explorer 9 and earlier versions.

注意:Internet Explorer 9 及更早版本不支持按钮标记的 formaction 属性。

回答by Shailesh Sonare

The best way to deal with multiple submit button is using switch casein action script

处理多个提交按钮的最佳方法是在动作脚本中使用switch case

<form action="demo_form.php" method="get">

    Choose your favorite subject:

    <button name="subject" type="submit" value="html">HTML</button>
    <button name="subject" type="submit" value="css">CSS</button>
    <button name="subject" type="submit" value="javascript">Java Script</button>
    <button name="subject" type="submit" value="jquery">jQuery</button>

</form>

Action / Server Side script:

动作/服务器端脚本:

demo_form.php

demo_form.php

<?php

switch($_REQUEST['subject']) {

    case 'html': //action for html here
                break;

    case 'css': //action for css here
                break;

    case 'javascript': //action for javascript here
                        break;

    case 'jquery': //action for jquery here
                    break;
}

?>

Ref: W3Schools

参考:W3Schools