具有多个“动作”的 HTML 表单

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

HTML form with multiple "actions"

htmlformssubmit

提问by Samuel Stiles

I'm setting up a form wherein I need two "actions" (two buttons):

我正在设置一个表单,其中我需要两个“操作”(两个按钮):

1 - "Submit this form for approval"
2 - "Save this application for later"

1 - “提交此表格以供批准”
2 - “保存此申请以备后用”

How do I create an HTML form that supports multiple "actions"?

如何创建支持多个“操作”的 HTML 表单?

EG:

例如:

<form class="form-horizontal" action="submit_for_approval.php">
<form class="form-horizontal" action="save_for_later.php">

<form class="form-horizontal" action="submit_for_approval.php">
<form class="form-horizontal" action="save_for_later.php">

I need to combine these two options-for-submitting into one form.

我需要将这两个提交选项组合成一种形式。

I did some basic research but couldn't find a definitive answer as to whether or not this is possible, and/or any good resources to links for a workaround.

我做了一些基础研究,但找不到关于这是否可行的明确答案,和/或任何好的资源链接以解决解决方法。

Thanks in advance.

提前致谢。

回答by isaacparrot

As @AliK mentioned, this can be done easily by looking at the value of the submit buttons.

正如@AliK 提到的,这可以通过查看提交按钮的值轻松完成。

When you submit a form, unset variables will evaluate false. If you set both submit buttons to be part of the same form, you can just check and see which button has been set.

当您提交表单时,未设置的变量将评估为 false。如果您将两个提交按钮都设置为同一表单的一部分,您只需检查并查看已设置的按钮。

HTML:

HTML:

<form action="handle_user.php" method="POST" />
  <input type="submit" value="Save" name="save" />
  <input type="submit" value="Submit for Approval" name="approve" />
</form>

PHP

PHP

if($_POST["save"]) {
  //User hit the save button, handle accordingly
}
//You can do an else, but I prefer a separate statement
if($_POST["approve"]) {
  //User hit the Submit for Approval button, handle accordingly
}

EDIT

编辑



如果您不想更改 PHP 设置,请尝试以下操作: http://pastebin.com/j0GUF7MVhttp://pastebin.com/j0GUF7MV


这是@AliK 所指的 JavaScript 方法。

Related:

有关的:

回答by Robert Blasco Villarroya

the best way (for me) to make it it's the next infrastructure:

使其成为下一个基础设施的最佳方式(对我而言):

<form method="POST">
<input type="submit" formaction="default_url_when_press_enter" style="visibility: hidden; display: none;">
<!-- all your inputs -->
<input><input><input>
<!-- all your inputs -->
<button formaction="action1">Action1</button>
<button formaction="action2">Action2</button>
<input type="submit" value="Default Action">
</form>

with this structure you will send with enter a direction and the infinite possibilities for the rest of buttons.

使用此结构,您将发送输入方向和其余按钮的无限可能性。

回答by Rj Acain

this really worked form for I am making a table using thymeleaf and inside the table there is two buttons in one form...thanks man even this thread is old it still helps me alot!

这确实有效,因为我正在使用百里香叶制作一张桌子,桌子内有两个按钮,一种形式......谢谢伙计,即使这个线程很旧,它仍然对我有很大帮助!

<th:block th:each="infos : ${infos}">
<tr>
<form method="POST">
<td><input class="admin" type="text" name="firstName" id="firstName" th:value="${infos.firstName}"/></td>
<td><input class="admin" type="text" name="lastName" id="lastName" th:value="${infos.lastName}"/></td>
<td><input class="admin" type="email" name="email" id="email" th:value="${infos.email}"/></td>
<td><input class="admin" type="text" name="passWord" id="passWord" th:value="${infos.passWord}"/></td>
<td><input class="admin" type="date" name="birthDate" id="birthDate" th:value="${infos.birthDate}"/></td>
<td>
<select class="admin" name="gender" id="gender">
<option><label th:text="${infos.gender}"></label></option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
<td><select class="admin" name="status" id="status">
<option><label th:text="${infos.status}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td><select class="admin" name="ustatus" id="ustatus">
<option><label th:text="${infos.ustatus}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
<td><select class="admin" name="type" id="type">
<option><label th:text="${infos.type}"></label></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select></td>
<td><input class="register" id="mobileNumber" type="text" th:value="${infos.mobileNumber}" name="mobileNumber" onkeypress="return isNumberKey(event)" maxlength="11"/></td>
<td><input class="table" type="submit" id="submit" name="submit" value="Upd" Style="color: white; background-color:navy; border-color: black;" th:formaction="@{/updates}"/></td>
<td><input class="table" type="submit" id="submit" name="submit" value="Del" Style="color: white; background-color:navy; border-color: black;" th:formaction="@{/delete}"/></td>
</form>
</tr>
</th:block>