Reputation: 421
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)));
}
Upvotes: 3
Views: 2155
Reputation: 54409
As I can see you want to generate route by using URL::action
.
This should work:
$('#updateTabelaPerguntas').load(
'{{ URL::action("PerguntaController@getBlocosPergunta", "") }}' + '/' + idModelo
);
Upvotes: 1
Reputation: 24661
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);
Upvotes: 1