0

I have the next route in Symfony:

 path: /BuscarCriterio/{nombre}

How Can i pass the Variable {nombre} with JavaScript?

var nombre=$('#form_nombre').val();

            var Ruta= "{{ path('BuscarporCriterio',{'nombre':I need my JS Variable Here }) }}";

it is necessary to know i can't use window.location because i need this route for make a call to Ajax.

Thanks for your answer!

1 Answer 1

1

The FOSJsRoutingBundle can be used to generate paths in javascript files https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

Install the bundle and try this inside a js file

var nombre = $('#form_nombre').val();
var Ruta = Routing.generate('BuscarporCriterio', { nombre: nombre });
Sign up to request clarification or add additional context in comments.

8 Comments

Amazing! i did it. Thank you Very much.
This can't work ! var nombre is js, when used in path function it's twig.
@COil It was tested and it works if written inside a twig template like I said.
@DiegoBastidas You are welcome. Remember that you should write your js code in .js files, and to generate your paths use the FOSJsRoutingBundle.
Just tested your snippet; symfony exception: "Variable "nombre" does not exist." {% extends 'AppBundle::layout.html.twig' %} {% block content %} var nombre = $('#form_nombre').val(); var Ruta = "{{ path('home_page', { nombre: nombre }) }}"; {% endblock %}
|

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.