Request select returning null value

Asked

Viewed 297 times

0

Good afternoon, I’m having trouble getting the value of a select in the following form:

{{Form::open(array('action' => 'FilterController@EstatisticaEsic', 'class' => 'form-filter'))}}
            <div class="col-md-2 col-sm-12 col-xs-12 divEstatic-3">
               {{Form::label('mesEsic', 'Selecione o mês')}} 
            </div>
            <div class="col-md-2 col-sm-12 col-xs-12 divEstatic-4 selectEstatic">
                {{Form::select('mesEsic', array('0' => 'Selecione', '01' => 'Janeiro', '02' => 'Fevereiro',
                            '03' => 'Março', '04' => 'Abril', '05' => 'Maio', '06' => 'Junho', '07' => 'Julho',
                            '08' => 'Agosto', '09' => 'Setembro', '10' => 'Outubro', '11' => 'Novembro',
                            '12' => 'Dezembro'), null, ['class' => 'form-control estaticForm'])}}


        {{Form::submit('Visualizar', ['class' => 'btn btn-info btnEstatic'])}}
    </div>
    {{Form::close()}}

That done, I created the route:

Route::post('/EstatisticaEsic', 'FilterController@EstatisticaEsic');

And the way in the controller:

public function EstatisticaEsic(Request $request)
    {
//        $mes = ($request->mesEsic ? $request->mesEsic : 0);
//        $mes = Input::get('mesEsic');
        $mes = $request->input('mesEsic');

        $table = DB::table('pedido_esic')
//                ->where(DB::raw("SUBSTR(created_at, 6, 2)"), '=', $mes)
                ->select(DB::raw("(Select count(id) from pedido_esic) as a"), 
                        DB::raw("(Select count(id) from pedido_esic where situacao = 'Respondido') as b"),
                        DB::raw("(Select count(id) from pedido_esic where situacao = 'Em Tramitação') as c"))
                ->get();


//        return $mes;
        return ['table' => $table[0], 'mes' => $mes];
    }

So I created a javascript function by taking the route object and filling certain Ivs:

$(function (){
    $('.btnEstatic').on('click', function (event){

        event.preventDefault();

//        $.post($(this).attr('href'))
            $.post('../public/EstatisticaEsic')
                .done(function (json){
                console.log(json.mes);
                        $('.divEstaticRegis').html('');
                        $('.divEstaticResp').html('');
                        $('.divEstaticIndef').html('');

                        $('.divEstaticRegis').html(json.table.a);
                        $('.divEstaticResp').html(json.table.b);
                        $('.divEstaticIndef').html(json.table.c);
                }).error(function (json){console.log(json);
                $('body').html(json.responseText);});
    });
});

The problem is that my select in the form does not match as it should, its return is only as "null" tried, tried and failed, when I have returned the variable $mes by the controller itself returns me correctly, but if I play it inside the database search or as an object in the java script the return is always "null", could someone please help me? What am I missing?

  • In EstatisticaEsic you wouldn’t have to give a echo json_encode(['table' => $table[0], 'mes' => $mes]) in place of Return ?

  • I don’t understand where I use what you mentioned, but I don’t think it’s necessary, since apparently my problem is just why my $request->mesEsic isn’t working in the controller when I utulizo inside the sql query.

  • @rray is right, I think that’s where the error comes from, however in Laravel is more complex than that, should send the token also in ajax, and then in server side does: return response()->json(['table' => $table[0], 'mes' => $mes]); and then in ajax you must do the answer code var dados = JSON.parse(json); that will give a JS obj and can access so: dados.mes;. Here are some references: http://engageinteractive.co.uk/blog/csrf-protection-with-ajax-and-laravel, http://stackoverflow.com/questions/32738763/laravel-csrf-token-mismatch-forajax-post-request

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.