Two forms - two submit buttons. How does the controller identify which button was pressed?

I have two forms with two submit buttons on the same page, one view and one controller should manage them. I want him to perform one action if the first clicked, and another action if the second. I tried this when editing is the form name, but it does not work:

if($this->getRequest()->get('edit')) 

I also tried setting the value to the submit buttons, but I couldn't do it either. Please help me find a way to determine which button was clicked. :)

+4
source share
3 answers

Give the buttons different attributes "name" (not "id")

 <form ...> ... <input type="submit" name="btnA" value="ActionA"> <input type="submit" name="btnB" value="ActionB"> </form> 

Then the controller should analyze the POST data for the variable whose name will be the name of the button:

 if (isset($_POST['btnA'])) { /* do A */ } else if (isset($_POST['btnB'])) { /* do B */ } 
+5
source

Symfony 2.3 comes with a solution. It supports buttons in forms, and you have the isClicked () method to check if a button has been clicked.

http://symfony.com/blog/new-in-symfony-2-3-buttons-support-in-forms

+13
source

You can use hidden fields and check their values ​​:)

+2
source

All Articles