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

时间:2022-09-25 15:22:13

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:

我有一个网站,我想要有两个单独的提交按钮,其中一个将数据输入,并做一些计算,以显示在同一个屏幕上。我成功地做到了:

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

现在我想要第二个提交按钮,该按钮仍然可以获取他们输入的数据,但随后会转到另一个地址。有没有一种优雅的方式来做这件事?

3 个解决方案

#1


46  

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

您可以向新的submit按钮添加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>

#2


13  

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

可以使用formaction="page1 "更改表单动作。在button属性中。

<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属性。

#3


7  

The best way to deal with multiple submit button is using switch case in 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

#1


46  

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

您可以向新的submit按钮添加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>

#2


13  

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

可以使用formaction="page1 "更改表单动作。在button属性中。

<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属性。

#3


7  

The best way to deal with multiple submit button is using switch case in 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