Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
178
rated 0 times [  180] [ 2]  / answers: 1 / hits: 10235  / 10 Years ago, thu, july 10, 2014, 12:00:00

I would like to know how to pass javascript variables values from twig to controller using href path in Symfony2. this is my code:



 <html>
<head>
<script src=//code.jquery.com/jquery-1.11.1.min.js></script>
</head>
<body>
<script>
var a = hello;
var b = hi;
var c = $('<a href={{ path('ikproj_groupe_homepagegroupe1', {'id': a, 'num': b}) }}>send data</a>');
$('body').append(c);
</script>
</body>
</html>


The problem is that it displays this message: Variable a does not exist .
So, my question is: what is the correct code to do that?


More From » symfony

 Answers
5

You need to set a twig variable. Ex:



<script>
{% set a = 'hello' %}
{% set b = 'hi' %}
var c = $('<a href={{ path('ikproj_groupe_homepagegroupe1', {'id': a, 'num': b}) }}>send data</a>');
$('body').append(c);
</script>

[#43986] Wednesday, July 9, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kareem

Total Points: 733
Total Questions: 110
Total Answers: 102

Location: Bermuda
Member since Thu, Apr 20, 2023
1 Year ago
;