Javascript 带有两个按钮的表单以不同的值提交

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

Form with two button submit with different value

phpjavascriptjqueryhtmlforms

提问by Mark Fondy

<form action="here.php" method="POST">
<input type="text" name="text">

<div id="one">
   <input type="hidden" name="aaa" value="one">
   <input type="submit" value="Send">
</div>

<div id="two">
   <input type="hidden" name="aaa" value="two">
   <input type="submit" value="Send">
</div>

</form>

Now if i click on Send of div ONE or div TWO i have always in $_POST['aaa'] = 'two';

现在,如果我点击发送 div ONE 或 div TWO,我总是在$_POST['aaa'] = 'two';

Is possible make one form with two submit with different values?

是否可以制作一个带有两个提交不同值的表单?

If i click on div one submit i would like reveice $_POST['aaa'] = 'one'and if i click on div two submit i would like receive $_POST['aaa'] = 'two'.

如果我点击 div one 提交,我希望收到 $_POST['aaa'] = 'one',如果我点击 div 2 提交,我希望收到$_POST['aaa'] = 'two'

How can i make it?

我怎样才能做到?

I can use for this PHP and jQuery.

我可以使用这个 PHP 和 jQuery。

EDIT: I dont want create two form - i dont want showing two many times <input type="text" name="text">

编辑:我不想创建两个表单 - 我不想多次显示两个 <input type="text" name="text">

EDIT: maybe i can instead button submit ? but how?

编辑:也许我可以改为按钮提交?但是怎么样?

回答by dmon

It seems that what you actually want to do is have a value in each of the buttons, see this, for example:

看来,你真正想要做的是在每个按钮的价值,看到这个,例如:

<form action="demo_form.asp" method="get">
  Choose your favorite subject:
  <button name="subject" type="submit" value="fav_HTML">HTML</button>
  <button name="subject" type="submit" value="fav_CSS">CSS</button>
</form>

回答by Marc B

You'd need two different forms:

你需要两种不同的形式:

<div id="one">
   <form ...>
      <input type="hidden" name="aaa" value="one">
      <input type="submit" value="Send">
   </form>
</div>

<div id="two">
    <form ...>
       <input ...>
       <input ...>
    </form>
</div>

Standard practice is that when two fields have the exact same name, to use only the LAST value encountered in the form and submit that.

标准做法是,当两个字段完全相同时name,只使用表单中遇到的最后一个值并提交。

PHP does have a special-case notation (name="aaa[]") to allow submitting multiple values with the same name, but that wouldn't help you here, as that'd submit ALL of the aaavalues, not just the one closest to the submit button.

PHP 确实有一个特例符号 ( name="aaa[]") 来允许提交具有相同名称的多个值,但这在这里对您没有帮助,因为它会提交所有aaa值,而不仅仅是最靠近提交按钮的值。



HTML form:

HTML表单:

<form ...>
<input type="text" name="textfield">

<div id="one">
   <input type="hidden" name="one_data" value="aaa" />
   <input type="submit" name="submit_one" value="Submit" />
</div>

<div id="two">
   <input type="hidden" name="two_data" value="bbb" />
   <input type="submit" name="submit_two" value="Submit" />
</div>
</form>

server-side:

服务器端:

if (isset($_POST['submit_two'])) {
    $data = $_POST['two_data'];
} else if (isset($_POST['submit_one'])) {
    $data = $_POST['one_data'];
} else {
    die("Invalid submission");
}

回答by Gaurav

Instead of showing two submit button, you can show a radio list with two options and one submit button.

您可以显示带有两个选项和一个提交按钮的单选列表,而不是显示两个提交按钮。

回答by ParPar

Try this:

尝试这个:

in html-

在 html-

    <input id="PreviousButton" value="Previous" type="submit" />
    <input id="NextButton" value="Next" type="submit" />

    <input id="Button" name="btnSubmit"  type="hidden" />

in jOuery-

欣喜若狂——

        $("#PreviousButton").click(function () {
            $("#Button").val("Previous");
        });

        $("#NextButton").click(function () {
            $("#Button").val("Next");
        });

then you can see in the form results - what "Button" contains.

然后您可以在表单结果中看到 - “按钮”包含的内容。