tags:

views:

70

answers:

5

I have 2 buttons in HTML form. And both of them should submit the form. I would need to capture which button has been clicked so i can use it to perform different actions based on which button was clicked.

I am able to submit the form with both the buttons but how would i capture which button was clicked in the php file .

<INPUT  type="button" name="submit" class="button" class="element_2" value="firstbutton">
<INPUT  type="button" name="submit1" class="button" class="element_2" value="second button.. ">

i am using post method in Jquery to submit the form. How can i check which HTML button was clicked in server side php script

+1  A: 

You could try using the isset function

if(isset($_POST['submit'])){
echo "first button was clicked";
}

Or to detect the second one was clicked:

if(isset($_POST['submit1'])){
echo "second button.. was clicked";
}
hart1994
See @Nick comment on my deleted answer as to why this is wrong.
Darin Dimitrov
@Darin - 53 rep ;) I'll repro here though.
Nick Craver
This isn't correct, he's using the post method in jQuery, even if using `.serialize()`, it explicitly outlines this doesn't happen: "Note: Only "successful controls" are serialized to the string. No submit button value is serialized since the form was not submitted using a button. For a form element's value to be included in the serialized string, the element must have a name attribute. Data from file select elements is not serialized." Reference: http://api.jquery.com/serialize/
Nick Craver
@Nick Thanks for pointing out the reason why Darin's earlier solution would not work.
Prady
+2  A: 

You could use a hidden element here, something like this:

<input type="hidden" id="submittedBy" name="submittedBy">

Your current .submit() handler using .serialize() and $.post() should work, just update the hidden field when clicking either button, for example:

$("form :submit").click(function() {
  $("#submittedBy").val(this.name);
});

Then in PHP just check that $_POST["submittedBy"] == "submit" or "submit1" to see which caused it.


The alternative is to have the click handler POST and add in the value between .serializeArray() and when $.param() is called, like this:

$("form :submit").click(function() {
  var data = $(this.form).serializeArray();
  data.push({ name: 'submittedBy', value: this.name });
  $.post("myPage.php", data, function(result) {
    //do something
  });
  return false; //prevent normal form submission, even with the .submit() handler
});
Nick Craver
+1  A: 

I would capture all the events on the page using:

$(document).click(function (obj) {
    if ('equipmentSetup' === obj.target.id) {
        $('#form').submit();
    }

....

Then add an if statement looking for the "id" of the button you want. Don't use the name or id "submit". I forgot why but I remember it caused problem for me.

Clutch
+1  A: 

Just do this,

HTML

<button type="submit" name="action[update]" value="1">Update</button>
<button type="submit" name="action[delete]" value="1">Delete</button>

PHP

$action = isset($_POST['action']['update']) ? 'update' : 'delete';

You CAN'T depend on JavaScript to tell you wich button was clicked, if user has JavaScript disabled, your form is broken.

xmarcos
If you have more than two, the same logic applies, but you migh want to use a switch statement.
xmarcos
+1  A: 

You could try changing the buttons to type="button" and give them ids. With that, you can use the jquery line (not able to check my syntax below, but think i've got it right)

$('form button').click(function(){
    if($(this).attr('id') = "button1"){ ...button 1 clicked}
    ..process form here..
});

would that do it?

Aninemity