Form with two button submit with different value
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.
Mark Fondy
Updated on October 08, 2020Comments
-
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 over 12 yearsthis is OK, but i dont want showing two times on my page <input type="text" name="text">
-
Marc B over 12 yearsYou 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 over 12 yearsthanks for edit (+1), but what if this submit button is for example 20? i must check this seperately?
-
Marc B over 12 yearsYou mean 20 different submit buttons? Ouch, yes, you'll have to check each one.
-
Tom Collins over 10 yearsIt'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).