2

so I need to send array to other page. I was tried to make it by form:

<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty" value = "{{ sniadanie }}">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>

"sniadanie" is array, for example it works as: sniadanie[0]['ilosc']

On page "przepisy" I tried to use this code:

{% set produkty = app.request.get('produkty') %}
{{produkty[0]['iloscuser']}}

But it doesn't work. Somebody have idea how can I do this?

1
  • do you absolutely must need to use form to send the array value ? if not a must, you can use session. if u must use form, use a loop function to extract all the array value and put it in the "hidden input value"
    – cyan
    May 17, 2015 at 15:26

1 Answer 1

2

This is not specific to Symfony2. Its basic HTML. You have to supply multiple input with appropriate name. You cannot sent an array value in a single input element!! Thats basic HTML!!

<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty[0][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[1][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[2][iloscuser]" value = "specific-value-from-sniadanie">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>

But you can send a json string inside a single element.

<form action="{{ path('_przepisy') }}" method="post">
    <input type="hidden" name = "produkty" value = "{{ sniadanie | serialize }}">
    <input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>

Then in controller

$this->render("your view", [
    'produkty'  => json_decode($request->get('produkty'))
]);

And in the template

{{produkty[0]['iloscuser']}}
1
  • Thanks for help :) I decided to do it in other way, I just use now in controler: return $this->render('MainBundle:Default:przepisy.html.twig', array( "sniadanie" => $sniadanie)); And works ok too :P
    – Mateusz
    May 17, 2015 at 16:35

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.