Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get data from serialized string in Symfony2 controller

i'm trying to figure out how to get the data out of the serialized string of the submitted form.

$(document).ready(function() {
    $("#formC").on('submit', function(e) {
         var url = $(this).attr("action");

        e.preventDefault();

        $.post(url,{
            data: $("#formC").serialize(),
            type: 'post'
        }).done(function(data) {
            alert(data);
        });
    });

Form:

<form action="{{ path('advd_p_group_ps_rd_ss') }}" method="post" {{ form_enctype(formBase) }} id="formC">
                                    <thead>
                                        <tr>
                                            <th><input type="checkbox" id="selectall"></th>
                                            <th>{{ 'general.date'|trans }}</th>
                                            <th>{{ 'general.order_number'|trans }}</th>
                                            <th>{{ 'general.description'|trans }}</th>
                                            <th>{{ 'general.company_name'|trans }}</th>
                                            <th>{{ 'general.name'|trans }}</th>
                                            <th>{{ 'form.status'|trans }}</th>

                                        </tr>
                                    </thead>
                                        {% for details in details %}
                                    <tbody>
                                        <tr>

                                            <td><div align="center"><input type="checkbox" class="selectedId" name="selectedId" value="{{details.id}}" onclick="javascript:resetSelectAll();" /></div></td>
                                            <td>{{ details.date | date("m/d/Y") }}</td>
                                            <td>{{ details.order_number }}</td>
                                            <td>{{ details.description }}</td>
                                            <td>{{ details.company }}</td>
                                            <td>{{ details.name }}</td>
                                            <td>{{ details.status }}</td>


                                        </tr>
                                    </tbody>
                                        {% endfor %}

                            </table> 

                        </div>
                    </div>
                {% if info == 1 %}
                    <div id="information">
                        <div id="tableContent">

                                    {{ form_widget(form) }}
                            <input class="input_button" type="submit" value="Procesar" id="procesar" />
                            </form>

Controller:

public function groupBatchPrizesRequestedAction(Request $request){
   if ( $request->getMethod() == 'POST' ) {
        $data = $request->get('data');
}

The output when I do die($data) is something like this:

selectedId=23&selectedId=25&requested%5Bstatus%5D=11&requested%5Bcomments%5D=sdsds&requested%5B_token%5D=e40bc826cce8b756ecce002181301e951e588028

My question is, how do I get all "selectedId", requested_status and comments from it? Is there a way to send it as an array, an array it's easier to handle...

like image 327
Splendonia Avatar asked Aug 16 '13 15:08

Splendonia


2 Answers

Use serializeArray function to correctly submit your data.

$.post(url,{
    data: $("#formC").serializeArray(),
    type: 'post'
})
like image 54
Alexey B. Avatar answered Nov 15 '22 03:11

Alexey B.


Use in your controller:

$request = $this->get('request');
$data = $request->request->all();
like image 37
tuenti Avatar answered Nov 15 '22 04:11

tuenti