Popolitus
Popolitus

Reputation: 463

how can pass javascript variable to twig?

var prova= $("select#utente_regione_name").val();
$.ajax({
    type: "POST",
    url: "{{ path('province', { 'variabile': 'prova<------it's correct?' }) 
}}", 

Upvotes: 2

Views: 7859

Answers (3)

Miles M.
Miles M.

Reputation: 4169

Using FOSJsRoutingBundle will do the job fine. - that's precisely why it has been made for.

With it you can use router from JS:

Routing.generate('my_route_to_expose', { "id": 10, "foo": "bar" });

Upvotes: 0

mikayel ghazaryan
mikayel ghazaryan

Reputation: 716

You can pass it as an attribute, like this:

var prova= $("select#utente_regione_name").val();
$.ajax({
    type: "POST",
    url: "{{ path('province') }}",
    data: { variable: prova },
}}", 

of course that means that in Controller you should read it from

$variable = $this->getRequest()->get('variable'); 

and remove that from your route. I don't think it's so crucial to have that parameter in route for Ajax routes.

PS. I would appreciate if somebody will post how to do this in a true Symfony way, because this still looks like workaround for me.

Upvotes: 4

Seagull
Seagull

Reputation: 3600

You can't access data which comes in array $_POST in twig. You should change you script to pass variable from $_POST to you twig template.

Upvotes: 0

Related Questions