Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form with two button submit with different value

<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?

like image 566
Mark Fondy Avatar asked Jan 29 '12 15:01

Mark Fondy


3 Answers

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>
like image 177
dmon Avatar answered Oct 16 '22 13:10

dmon


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");
}
like image 45
Marc B Avatar answered Oct 16 '22 12:10

Marc B


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

like image 3
Gaurav Avatar answered Oct 16 '22 14:10

Gaurav