HTML 表单可以有两个动作而不是一个动作吗?

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

Can an HTML form have two actions instead of one?

htmlforms

提问by Jonis

An HTML button looks like this:

一个 HTML 按钮如下所示:

<form name="HAHA" id="login_form" action="show.php" method="post">

The actionof this button is show.php. That is just 1 action.

action这个按钮是show.php。那只是1个动作。

How would I use two actions?

我将如何使用两个动作?

<form name="HAHA" id="login_form" action="show.php","show2.php" method="post">

Does this work? Now I have 2 actions. Or do I have to use an Array?

这行得通吗?现在我有 2 个动作。还是我必须使用数组?

回答by Thomas1703

If I understand your question right, you could create a new_action.phpwith a code like this:

如果我理解你的问题是正确的,你可以new_action.php用这样的代码创建一个:

<?php
  include('show.php');
  include('show2.php');
?>

in HTML you have to change the action attribute to

在 HTML 中,您必须将 action 属性更改为

<form method="POST" action="new_action.php" name="form" id="form">

回答by Michas

No.

不。

The actionis an URL to send values from the form and get the response. The browser can process only one response. There are two solution for this limitation.

action是从形式发送值,并获得响应的URL。浏览器只能处理一个响应。这个限制有两种解决方案。

  1. Send form by asynchronous JavaScript and write own logic to process responses.
  2. Create one server side script as one, new action. The script process work of those many actions You want to put into form.
  1. 通过异步 JavaScript 发送表单并编写自己的逻辑来处理响应。
  2. 创建一个服务器端脚本作为一个新的动作。脚本处理您要放入表单的许多操作。

回答by Sweet Life

As Thomas1703 said just make the single action and in the php file do everything.

正如 Thomas1703 所说,只需执行单个操作,并在 php 文件中执行所有操作。

example:

例子:

<form action="myphp.php" method="POST" />
  <input type="submit" value="lets say Hello"   name="action1"/>
  <input type="submit" value="lets say Welcome" name="action2">
</form>

PHP

PHP

if($_POST["action1"]) {
     echo "Hello";
}
//You can do an else, but I prefer a separate statement
if($_POST["action2"]) {
     echo "Welcome";
}

I hope that you have understand me.

我希望你理解我。