3

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.

1 Answer 1

4

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
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just that? And how i would write i want the parametet to be get when the template is submited? Could you please give me an example of how the controller action would look? @rayan.gigs? Thanks
Thanks @rayan.gigs! And the call to the template will be done afterwards inside the controller? How can i know the yemplate has been aubmited an so i can use te value in te argument?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.