3

I'm trying to update a section of my page using this code. I have a select element #sModelo that every time that it changes, updates a table that i have. The problem is that when i change the select the load function receives http://localhost:8000/admin/auditorias/pesquisa/+idModelo+" instead of http://localhost:8000/admin/auditorias/pesquisa/1 (1 is the value from idModelo)

Route:

Route::get('admin/auditorias/pesquisa/{idModelo}', array('uses' => 'PerguntaController@getBlocosPergunta', 'as'=>'pergunta.pergunta'));

Blade code:

<div class="form-group @if ($errors->has('modelo')) has-error @endif">
    <select id="sModelo" name="modelo" class="form-control" style= "width: 150px">
        <option value="" disabled selected> Modelo </option>
        @foreach ($modelos as $modelo)
            <option value="{{$modelo->id }}">  {{$modelo->nome }}</option>
        @endforeach
    </select>
</div>
...
<table id="updateTabelaPerguntas" class="table table-bordered table-condensed alinhar">
    @include('pergunta.pergunta')
</table>

Jquery code:

$("#sModelo").on('change', function(e){
    var idModelo = e.target.value;
    console.log(idModelo); //shows the value 1
    $('#updateTabelaPerguntas').load('{{ URL::action("PerguntaController@getBlocosPergunta",array("idModelo"=>'+idModelo+')) }}');
});

Controller:

public function getBlocosPergunta($idModelo)
{
    return View::make('pergunta.pergunta', array('blocos'=>Pergunta::getBlocosPergunta($idModelo)));
}
2
  • where's you jquery code? in blade view? Commented Apr 7, 2015 at 17:55
  • yes its in the blade page Commented Apr 7, 2015 at 17:57

2 Answers 2

1

As I can see you want to generate route by using URL::action.

This should work:

$('#updateTabelaPerguntas').load(
  '{{ URL::action("PerguntaController@getBlocosPergunta", "") }}' + '/' + idModelo
);
Sign up to request clarification or add additional context in comments.

Comments

1

This line is your issue:

$('#updateTabelaPerguntas')
    .load('{{ URL::action("PerguntaController@getBlocosPergunta",array("idModelo"=>'+idModelo+')) }}');

You're basically mixing PHP and JavaScript statements here. When that page is accessed, everything in between the {{ }} markers is evaluated on the server prior to anything being returned back to the client. At that point, the server has no idea what the user chose, and '+idModelo+' isn't a valid part of any PHP expression.

I believe if you inspect the source of your document (as it is returned by the server) then the jQuery code will look like this:

$('#updateTabelaPerguntas')
    .load('http://localhost:8000/admin/auditorias/pesquisa/+idModelo+');

Which is exactly the behavior you're experiencing. My suggestion would be to just use the actual URL, and update the ID in the URL based off of what the user selected in JavaScript:

$('#updateTabelaPerguntas')
    .load('/admin/auditorias/pesquisa/' + idModelo);

1 Comment

Thank's for the explanition! I try the solution of @limonte and it works!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.