Reputation: 2140
I want to get the value of an input field from my template twig with twig not with javascript. My input field hasn't a form, it is a hidden input.
{% block body -%}
<input type="hidden" name="id_client" id="id_client" value="123"/>
...
{% endblock %}
I want to do this:
{{ path('client_view',{"id": value_Of_Hidden_Input}) }}
How can I get value_Of_Hidden_Input
EDIT:
My requirement:
I have a list of customers and I have for each customer a button to show details of customer "Modifier coordonnées"
.
I want on clicked one customer a function AJAX will executing the action showAction
This is my code:
{% block body -%}
<div class="tab-client">
<table id="mytable" class="table table-bordred table-striped" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>Raison Sociale</th>
<th>Identifiant</th>
<th>M. de passe</th>
<th>Solde actuel</th>
<th class="text-center sty-td-action">Action</th>
</tr>
</thead>
<tbody>
{% for entity in entities %}
<tr>
<td>{{ entity.raisonSociale}} </td>
<td>{{ entity.login }}</td>
<td>{{ entity.password }}</td>
<td>{{ entity.soldeSMS }}</td>
<td>
<a class="modifier-client" id="modif_coordonnee"><span class="glyphicon glyphicon1">Modifier coordonnées</span></a>
<a href="#historique" class="modifier-client"><span class="glyphicon glyphicon2">Voir Historique</span></a>
<a href="#ajout" class="modifier-client"><span class="glyphicon glyphicon3">Ajout transaction</span></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div><!--tab-client-->
{% endblock %}
{% block javascripts %}
<script>
$("#modif_coordonnee").click(function() {
$.ajax({
//On lui indique le type d'envoie des informations
type: 'POST',
//On lui indique le chemin de la fonction
url: '{{ path('client_show',{"id": value_of_id_client}) }}',
//On lui donne la valeur du choix qu'on a fait, et id est la variable qui va contenir notre valeur, nous la retrouvons dans notre controller
//Enfin nous lui disons de remplir notre formulaire avec le resultat
success: function(response)
{
......
}
}
)});
</script>
{% endblock %}
My problem is how can determined value_of_id_client
?
Upvotes: 0
Views: 16139
Reputation: 1772
I think the correct way to handle this is in the controller.
When you post the form, take that hidden value and inject it into your template.
Pull id_client from the Request object.
$this->render('TEMPLATE', array('client_id'=>$request->query->get('id_client'));
Then in your template, you can use the client_id variable.
Upvotes: -1
Reputation: 23580
First of all: An id
must be unique throughout the whole document. If you have multiple targets use the class
-attribute instead. I also added the current url
including the corresponding id
to each entity in your loop, using the HTML5 data
-attribute:
{% for entity in entities %}
[…]
<a class="modifier-client handler" data-href="{{ path('client_show',{ 'id': entity.id }) }}">
<span class="glyphicon glyphicon1">Modifier coordonnées</span>
</a>
[…]
{% endfor %}
In your JavaScript part, you have to change the selector and retrieve the url of the currently clicked element:
$('.handler').click(function() {
var href = $(this).data('href');
$.ajax({
[…]
url: href,
[…]
));
});
Upvotes: 1
Reputation: 16502
You need to use FOSJsRoutingBundle to generate a proper Symfony path that takes in an argument and places it in the correct portion of the URL (depending on your routes, your variable portion may be somewhere in the middle of the URI!)
You must follow all the installation instructions for the bundle, and make sure you expose the route in question.
Then you pull the value into the Routing.generate
function by passing the value retrieved by jQuery:
url: Routing.generate('client_show', {id: $('#id_client').val()}),
Upvotes: 0
Reputation: 41
You can't do this that way, try(js):
{{path('client_view')}}/+$("#id_client").val()
Upvotes: -1
Reputation: 696
try this .
{{ path('client_view',{"id": form.name_field_hidden.vars.value}) }}
Have more information on http://symfony.com/doc/current/reference/forms/twig_reference.html
Upvotes: 4