php 一种带有两个提交按钮和每个按钮不同操作的表单

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

One form with two submit buttons and different actions for each button

phpforms

提问by Nick

I have spent several hours trying to find a solution to my issue, but just can't seem to find the proper solution. Thanks in advance for your assistance!

我花了几个小时试图找到我的问题的解决方案,但似乎无法找到合适的解决方案。提前感谢你的帮助!

I have ONE html form with:

我有一个 html 表单:

<form id="columnarForm"  action="formindb_hoh_1.php" enctype="multipart/form-data" method="post" />

I would like to have TWO submit buttons:

我想要两个提交按钮:

<input type="image" name="camper" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />

I would like the first submit button to have the action of formindb_hoh_1.php when clicked and the second submit button to have the action of formindb_hoh_2.php, but am unsure how to make one button have one action and have the other button has a different action.

我希望第一个提交按钮在点击时具有 formindb_hoh_1.php 的动作,第二个提交按钮具有 formindb_hoh_2.php 的动作,但我不确定如何使一个按钮具有一个动作而另一个按钮具有不同的动作行动。

采纳答案by Redzwan Latif

Refer this :

参考这个:

Multiple submit buttons php different actions

多个提交按钮php不同的动作

Put this script in your page :

将此脚本放在您的页面中:

<script>
    function submitForm(action)
    {
        document.getElementById('columnarForm').action = action;
        document.getElementById('columnarForm').submit();
    }
</script>

Modify your input code :

修改您的输入代码:

<input type="image" name="camper" onclick="submitForm('formindb_hoh_1.php')" value="camper" src="../images/apps/camperBtn.png" class="submit_button" />
<input type="image" name="medical" onclick="submitForm('formindb_hoh_2.php')" value="medical" src="../images/apps/medicalBtn.png"class="submit_button" />

回答by szuflad

No JS, use HTML5.

没有 JS,使用 HTML5。

Change the submit buttons to use new HMTL5 button attribute formaction:

更改提交按钮以使用新的 HMTL5 按钮属性formaction

<button type="submit" name="camper" formaction="formindb_hoh_1.php">Camper</button>
<button type="submit" name="camper" formaction="formindb_hoh_2.php">Medical</button>

Reference: http://devdocs.io/html/element/button

参考:http: //devdocs.io/html/element/button

回答by Ashique Hussain Ansari

Finally, I got my answer after spending more than an hour. I tried a various method like switch case, jquery, and JavaScript. But I only used HTML5 formactiontag and now my form two submit button working at two location.

终于,我花了一个多小时后得到了答案。我尝试了各种方法,如 switch case、jquery 和 JavaScript。但我只使用了 HTML5formaction标签,现在我的表单两个提交按钮在两个位置工作。

Code :

代码 :

   <input type="submit" class="btn btn-success" value="Pay Now" name="submit"  style="width: 100%; letter-spacing: 1px; color: #fff; border: none;padding:  10px;" formaction="example.php">

   <button type="submit" class="btn btn-success" name="submit" style="width: 100%; letter-spacing: 1px; color: #fff; border: none;padding: 10px; margin-top: 15px;" formaction="mail.php"> Button</buttton>

回答by Hasnnnaiin

I have the same issue. but the thing is the first button function is in JS while the other is using PHP.

我有同样的问题。但问题是第一个按钮功能在 JS 中,而另一个使用 PHP。

$('#add-bookingid').submit(function(e){
e.preventDefault();
calculateDistance();
});

You see $('#add-bookingid').submit so both buttons have same button type as submit.

您会看到 $('#add-bookingid').submit,因此两个按钮与提交具有相同的按钮类型。

Main Purpose

主要目的

Button# 1

按钮#1

Button# 2

按钮#2