php 一个表单可以有多个动作吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2996806/
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
Can one form have multiple actions?
提问by Brook Julias
I am wanting to submit a form to different places based on selections made in the form. I had originally been planning to to send all to a central file/location and have it determine where to go next. However, I would like to do without this extra step if I could.
我想根据表单中的选择将表单提交到不同的地方。我最初计划将所有内容发送到一个中央文件/位置,并让它决定下一步要去哪里。但是,如果可以的话,我希望没有这个额外的步骤。
If the user wants to create/edit/delete elements go to page 1.
If the user wants to group/attach elements go to page 3.
如果用户要创建/编辑/删除元素,请转到第 1 页。
如果用户要分组/附加元素,请转到第 3 页。
I am trying to write a form builder. You can create/edit/delete forms, questions, and answers. I have everything for creating, editing, and deleting done. Those functions are performed without leaving the page, but now I am looking to assign answers to specific questions. The questions page and the answers page are separate. I am wanting to select a group of answers and submit an array of answer Ids (selected check boxes) to the question page where those Ids will then be assigned to a question. So basically the create, edit, and delete functions are on without leaving the page, but the assign function would be performed on a different page.
我正在尝试编写一个表单构建器。您可以创建/编辑/删除表单、问题和答案。我已经完成了创建、编辑和删除的所有操作。这些功能无需离开页面即可执行,但现在我希望为特定问题分配答案。问题页面和答案页面是分开的。我想选择一组答案并将一组答案 ID(选中的复选框)提交到问题页面,然后将这些 ID 分配给问题。所以基本上创建、编辑和删除功能是在不离开页面的情况下打开的,但分配功能将在不同的页面上执行。
if(empty($delRowID) || empty(updateRowID) || empty($groupRows)) {
qInsert();
}else{
if(!empty($delRowID)) qDelete($delRowID);
if(!empty(updateRowID)) qUpdate($updateRowID);
if(!empty($groupRows)) {
submit $groupRows to Question.php;
}
}
回答by marapet
No, a form has only one action.
不,一个表单只有一个操作。
But you have options:
但是你有选择:
Javascript
Javascript
However, you may change the action attribute with javascript:
但是,您可以使用javascript更改 action 属性:
<input type="submit" value="Edit" onclick="editform();return true;">
<input type="submit" value="Delete" onclick="deleteform();return true;">
together with a little javascript:
再加上一点javascript:
function editform() {
document.myform.action = '/edit';
}
function deleteform() {
document.myform.action = '/delete';
}
See also
也可以看看
- How to set form action through JavaScript?
- Different form ACTION depending on button pressed
- jquery, changing form action
Multiple forms
多种形式
If javascript is not an option for you, you may consider multiple forms in your page.
如果 javascript 不适合您,您可以考虑在您的页面中使用多个表单。
Multiple forms = multiple actions
Multiple forms = multiple actions
No problems with javascript disabled clients, and standards compliant.
javascript 禁用客户端没有问题,并且符合标准。
Multiple submit buttons - server side
多个提交按钮 - 服务器端
Or you may handle the distinction of editing or deleting on the server side. No javascript needed.
或者您可以在服务器端处理编辑或删除的区别。不需要javascript。
Add multiple submit buttons to your form and give them the same name but a different value:
将多个提交按钮添加到您的表单中,并为它们提供相同的名称但不同的值:
<input type="submit" name="btSubmit" value="Edit">
<input type="submit" name="btSubmit" value="Delete">
You then can retrieve the value of the button which has been clicked. In php, the following should do the job:
然后,您可以检索已单击的按钮的值。在 php 中,以下应该完成这项工作:
$_POST['btSubmit']
See http://www.chami.com/tips/internet/042599I.htmlfor an example with classic asp.
有关经典 asp 的示例,请参见http://www.chami.com/tips/internet/042599I.html。
回答by Your Common Sense
It is possible using JavaScript, but it's not recommended because some people turn JS off by default because of trojans and noisy behavior of some sites. It is considered polite to have your site working both with JS enabled and disabled.
可以使用 JavaScript,但不建议这样做,因为有些人会因为木马和某些站点的嘈杂行为而默认关闭 JS。让您的网站同时启用和禁用 JS 被认为是礼貌的。
Actually you don't need many form actions because every operation can be done using branching in the single form handler script.
Here is the very simple CRUD application example, performing displaying, editing, adding - all in one body and utilizing templates:
实际上,您不需要很多表单操作,因为每个操作都可以使用单个表单处理程序脚本中的分支来完成。
这是一个非常简单的 CRUD 应用程序示例,执行显示、编辑、添加 - 全部在一个主体中并使用模板:
index.php
索引.php
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
form.php
表单.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
list.php
列表.php
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
回答by Matt Andrews
Just send them to a central location and use logic to send them somewhere else depending on what they selected.
只需将它们发送到一个中心位置,然后根据他们选择的内容使用逻辑将它们发送到其他地方。
eg:
例如:
<?php
switch($_POST['choice']) {
case 'edit':
$page = 'edit.php';
break;
case 'group':
$page = 'group.php';
break;
}
header("Location: " . $page);
(obviously needs some input filtering and default values)
(显然需要一些输入过滤和默认值)
EDIT: as pointed out by Col. Shrapnelbelow, using header()for this is pretty pointless as it'll then wipe out your $_POST array - use include()instead, but be careful to only allow your own files and never let the user name the file which will be included (eg using a form field value to pick the included file).
编辑:正如下面的Col. Shrapnel所指出的那样,使用header()它是非常没有意义的,因为它会清除你的 $_POST 数组 -include()改用,但要小心只允许你自己的文件,永远不要让用户命名文件将被包含(例如,使用表单字段值来选择包含的文件)。
回答by Nathan
No, you can't have multiple actions just in the html, but you can use javascript to switch up the action depending on what button is hit.
不,您不能仅在 html 中有多个操作,但是您可以使用 javascript 根据点击的按钮来切换操作。
I would do something like this (in pseudo-mootools)
我会做这样的事情(在伪mootools中)
<form action='default.php'>
... form elements ...
<button onclick='editButtonClick()' value='edit'/>
<button onclick='deleteButtonClick()' value='delete'/>
</form>
function editButtonClick() {
$('form').action = 'editaction.php';
$('form').submit();
}
function deleteButtonClick) {
$('form').action = 'deleteaction.php';
$('form').submit();
}
回答by Chris Crew
If I was looking at doing this I would pass a vaule of what you want doing by POST.
如果我正在考虑这样做,我会通过 POST 传递您想要做的事情。
Then check that value against a set of functions and each function will do a different thing based on it selection
然后根据一组函数检查该值,每个函数将根据它的选择做不同的事情
回答by Quentin
No, it can't (at least not without depending on JavaScript).
不,它不能(至少在不依赖 JavaScript 的情况下不能)。
Submit to one URI, and have a dispatch routine pass the data off to different functions depending on which radio button (or whatever) is selected.
提交到一个 URI,并让调度例程根据选择的单选按钮(或其他任何按钮)将数据传递给不同的函数。
回答by RobertPitt
<input type="submit" value="Add" onclick="submitForm(this,'add')" />
<input type="submit" value="Update" onclick="submitForm(this,'update')" />
<input type="submit" value="Delete" onclick="submitForm(this,'delete')" />
var submitForm = function(context,uri)
{
form = contenxt.parent; //Go back to the form
form.action = uri; // Set the action
form.submit(); //Submit the form;
}
Java script is the best way to handle this, there's not really much alternative unless you create 3 sole forms.
Java 脚本是处理此问题的最佳方法,除非您创建 3 个单独的表单,否则没有太多选择。
回答by RichW
Post your form to a central formhandler script. On that page just use simple logic (using php in this instance) to redirect the user to the specific page you want
将您的表单发布到中央表单处理程序脚本。在该页面上只需使用简单的逻辑(在本例中使用 php)将用户重定向到您想要的特定页面
回答by Ajay Singh Rathore
on Submit page you can call CURL and give all Get or post variables to that url.
在提交页面上,您可以调用 CURL 并将所有 Get 或 post 变量提供给该 url。

