我在一个表单中有两个提交按钮。我如何确定哪一个击中了服务器端?
当前回答
也许这里建议的解决方案在2009年是有效的,但我已经测试了所有这些被好评的答案,没有一个在任何浏览器上都有效。
我发现唯一有效的解决方案是这样的(但我觉得用起来有点难看):
<form method="post" name="form">
<input type="submit" value="dosomething" onclick="javascript: form.action='actionurl1';"/>
<input type="submit" value="dosomethingelse" onclick="javascript: form.action='actionurl2';"/>
</form>
其他回答
将name定义为数组。
<form action='' method=POST>
(...) some input fields (...)
<input type=submit name=submit[save] value=Save>
<input type=submit name=submit[delete] value=Delete>
</form>
服务器代码示例(PHP):
if (isset($_POST["submit"])) {
$sub = $_POST["submit"];
if (isset($sub["save"])) {
// Save something;
} elseif (isset($sub["delete"])) {
// Delete something
}
}
Elseif非常重要,因为两者都将被解析。
处理多个提交按钮的最佳方法是在服务器脚本中使用切换案例
<form action="demo_form.php" method="get">
Choose your favorite subject:
<button name="subject" type="submit" value="html">HTML</button>
<button name="subject" type="submit" value="css">CSS</button>
<button name="subject" type="submit" value="javascript">JavaScript</button>
<button name="subject" type="submit" value="jquery">jQuery</button>
</form>
服务器代码/服务器脚本-你提交表单的地方:
文件demo_form.php
<?php
switch($_REQUEST['subject']) {
case 'html': // Action for HTML here
break;
case 'css': // Action for CSS here
break;
case 'javascript': // Action for JavaScript here
break;
case 'jquery': // Action for jQuery here
break;
}
?>
来源:W3Schools.com
这非常容易测试:
<form action="" method="get">
<input type="submit" name="sb" value="One">
<input type="submit" name="sb" value="Two">
<input type="submit" name="sb" value="Three">
</form>
只需将其放入HTML页面,单击按钮,然后查看URL。
由于您没有指定使用的服务器端脚本方法,因此我将给出一个适用于PHP的示例
<?php if(isset($_POST["loginForm"])) { print_r ($_POST); // FOR Showing POST DATA } elseif(isset($_POST["registrationForm"])) { print_r ($_POST); } elseif(isset($_POST["saveForm"])) { print_r ($_POST); } else{ } ?> <html> <head> </head> <body> <fieldset> <legend>FORM-1 with 2 buttons</legend> <form method="post" > <input type="text" name="loginname" value ="ABC" > <!--Always use type="password" for password --> <input type="text" name="loginpassword" value ="abc123" > <input type="submit" name="loginForm" value="Login"><!--SUBMIT Button 1 --> <input type="submit" name="saveForm" value="Save"> <!--SUBMIT Button 2 --> </form> </fieldset> <fieldset> <legend>FORM-2 with 1 button</legend> <form method="post" > <input type="text" name="registrationname" value ="XYZ" > <!--Always use type="password" for password --> <input type="text" name="registrationpassword" value ="xyz123" > <input type="submit" name="registrationForm" value="Register"> <!--SUBMIT Button 3 --> </form> </fieldset> </body> </html>
形式
当点击Login -> loginForm
当点击Save -> saveForm
当点击注册->注册表单
<form>
<input type="submit" value="Submit to a" formaction="/submit/a">
<input type="submit" value="submit to b" formaction="/submit/b">
</form>
推荐文章
- 如何使一个DIV不包装?
- CSS div元素-如何显示水平滚动条只?
- 检查是否选择了jQuery选项,如果没有选择默认值
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 使用“!”的含义是什么?重要的”?
- 资源解释为样式表,但以MIME类型text/html传输(似乎与web服务器无关)
- 复选框输入是否只在被选中时才发布数据?
- 是类型="文本/css"必须在<链接>标签?
- 如何将LaTeX与Markdown混合?
- 如何使<div>总是全屏?
- 如何中心div垂直内绝对定位的父div
- 创建一个可变长度的字符串,用重复字符填充
- 有效使用< >(锚标签)没有href属性?
- 如何将全局字体应用到整个HTML文档
- 有人能解释一下这个“双重否定”的把戏吗?