Form with two button submit with different value

34,090

Solution 1

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>

Solution 2

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.

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 aaa values, not just the one closest to the submit button.


HTML form:

<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");
}

Solution 3

Try this:

in 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.

Solution 4

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

Share:
34,090
Mark Fondy
Author by

Mark Fondy

Updated on October 08, 2020

Comments

  • Mark Fondy
    Mark Fondy over 3 years
    <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';

    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'.

    How can i make it?

    I can use for this PHP and jQuery.

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

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

  • Mark Fondy
    Mark Fondy over 12 years
    this is OK, but i dont want showing two times on my page <input type="text" name="text">
  • Marc B
    Marc B over 12 years
    You can alway use some javascript to copy that one input field's value into another hidden field in both forms when it's submitted. But as your form is structured right now, there's no way around this. There's OTHER tricks you can use, like giving each field unique names, and use that fact to detect which submit button was clicked, and then access only the appropriate value.
  • Mark Fondy
    Mark Fondy over 12 years
    thanks for edit (+1), but what if this submit button is for example 20? i must check this seperately?
  • Marc B
    Marc B over 12 years
    You mean 20 different submit buttons? Ouch, yes, you'll have to check each one.
  • Tom Collins
    Tom Collins over 10 years
    It's usually recommended that the answer be included here, instead of just a link, in case the link no longer works (like for me right now).