79

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"

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

EG:

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

4

4 回答 4

83

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

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

HTML:

<form action="handle_user.php" method="POST" />
  <input type="submit" value="Save" name="save" />
  <input type="submit" value="提交审批" name="approve" />
</form>

PHP

如果($_POST[“保存”]){
  //用户点击保存按钮,进行相应处理
}
//你可以做一个else,但我更喜欢单独的语句
如果($_POST[“批准”]){
  //用户点击提交审批按钮,进行相应处理
}

编辑


如果你不想改变你的 PHP 设置,试试这个:http://pastebin.com/j0GUF7MV
这是 @AliK 引用的 JavaScript 方法。

有关的:

于 2013-05-21T02:12:10.993 回答
28

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

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

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

于 2016-10-07T07:38:39.173 回答
0

这应该可以在不更改后端代码的情况下工作:

<form class="form-horizontal" action="submit_for_approval.php">
  <button>Submit for Approval</button>
  <button formaction="save_for_later.php">Save for Later</button>
</form>

接受的答案对我不起作用,因为我使用的是 Golang,并且显然默认的 Go 表单返回与空变量相同的缺失变量(作为空字符串)。

于 2021-12-21T22:00:58.680 回答
0

这真的很有效,因为我正在使用百里香制作一张桌子,桌子里面有两个按钮在一个表格中......谢谢男人,即使这个线程很旧它仍然对我有很大帮助!

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

于 2019-02-03T07:36:35.413 回答