Mateusz
Mateusz

Reputation: 331

How to send array using html form

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?

Upvotes: 2

Views: 3630

Answers (1)

Broncha
Broncha

Reputation: 3794

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']}}

Upvotes: 2

Related Questions