我在表单中添加了一个额外的“提交”按钮,我将这样使用。
我有一种不安的感觉,单凭PHP / HTML可能无法实现这一点,而且在表单开始POST数据之前我可能不得不使用一些Javascript来修改表单操作?
思想和想法?
由于
答案 0 :(得分:3)
为两个提交按钮指定相同的名称,但值不同。您可以检查php文件中的值。
实施例
<form action="something.php" method="post">
<input type="submit" name="submit" value="one">
<input type="submit" name="submit" value="two">
</form>
<强> something.php 强>
switch( $_POST['submit'] ) {
case 'one':
case 'two':
}
答案 1 :(得分:2)
您可以在没有javascript的情况下执行此操作。只需为您的提交按钮名称指定不同的值:
<button type="submit" name="btn" value="addItem">Add item</button>
<button type="submit" name="btn" value="finish">Finished</button>
现在,在您发布表单的脚本中,您可以通过检查$_POST['btn']
值来确定单击了哪个按钮并执行相应的操作。
答案 2 :(得分:2)
您可以使用JavaScript根据单击的按钮修改表单,或者您可以检查服务器端(即使用PHP)单击了哪个按钮并采取相应的行动。
提交按钮就像其他任何形式一样是表单输入,即您可以为其指定名称和值,您可以检查服务器端。
在客户端(即使用JavaScript),您可以将处理程序绑定到按钮的click事件,修改表单的action-attribute并将其提交到新地址。
以下是客户端示例:
<!doctype html>
<html>
<head>
<title>Form submit test</title>
</head>
<body>
<form action="baz.html" method="post">
<input id="bar" type="submit" class="button" value="bar.html" name="" />
<input id="foo" type="submit" class="button" value="foo.html" name="" />
</form>
<script>
// Find the two buttons from the DOM and assign them to separate variables
var barBtn = document.getElementById('bar'),
fooBtn = document.getElementById('foo');
// Click-handler for the buttons.
// NB! For this code to work as intended, it needs to run
// in the context of the button, otherwise, the this-keyword
// will not resolve correctly and this will result in an error
// NB2! This code also requires that a button's value will be
// the desired action handler. Usually you would probably not
// do this, but use the button's name/value to lookup the
// correct form action.
function modifyAction(e) {
this.form.action = this.value;
}
// Bind an event handler to an object
// NB! This is not code you should use in production
function bindEvent(target, event, callback) {
if (target.addEventListener) {
target.addEventListener(event, callback, false);
} else if (target.attachEvent) {
target.attachEvent('on' + event, callback);
}
}
// Delegate creates a wrapping closure which binds the
// original function's context to an object, i.e. ensuring
// the this-keyword always refers to the same object when
// the returned function is invoked.
function delegate(context, method) {
return function () {
return method.apply(context, arguments);
}
}
// Bind the click-event of the barBtb, and handle it
// with the modifyAction-function bound to the barBtn.
// I.e. run the modifyAction function, with the this-keyword
// bound to barBtn
bindEvent(barBtn, 'click', delegate(barBtn, modifyAction));
// Same as above for fooBtn
bindEvent(fooBtn, 'click', delegate(fooBtn, modifyAction));
</script>
</body>
</html>
为了完整起见,这里有一个jQuery示例:
<form action="baz.html" method="post">
<input id="bar" type="submit" class="button" value="bar.html" name="" />
<input id="foo" type="submit" class="button" value="foo.html" name="" />
</form>
<script>
// Jquery event-handlers are automatically bound to
// the element selected, so using "this" is safe
function modifyAction(e) {
this.form.action = this.value;
}
// Bind the click-event on all input with type=submit
$("input[type=submit]").click(modifyAction);
</script>