Haritz
Haritz

Reputation: 1752

Symfony2 and jquery ajax

I am developing an application using Symfony2 and Jquery as a JavaScript FW. I am using Twig for the templates. I render a template from the controller and after making a selection using the cursor in the template I would like the value of the selected tag to be returned to the controller when submitting using a submit button in the mentioned template.

I use the next Jquery function:

$("MatchedTag").click(function () 
 {
       $(this).toggleClass("highlight");

       var IdOfTag = this.id;  
       $.ajax({
          url: "{{ path('AcmeAcmeBundle_myaction') }}",
          type: "POST",
          data: { "tag_id" : idOfTag },
          success: function(data) {
             //(success) do something...
             //variable "data" contains data returned by the controller. 
          }
       });
});

I guess in the controller, in myaction I should use something like $_POST["tag_id"] or getrequest() , bindrequest() to get the value but I dont really know how. Could someone give me an example. Thanks.

Upvotes: 3

Views: 4869

Answers (1)

Sameh Serag
Sameh Serag

Reputation: 746

You can try to get this parameter by :

 $request->request->get('tag_id');

Update simple action

namespace Acme\HelloBundle\Controller;
use Symfony\Component\HttpFoundation\Response;

class HelloController
{
   public function indexAction($name)
   {
     $myParam = $request->request->get('tag_id');
     // write your code here
   }
}

Upvotes: 4

Related Questions