Reputation: 794
I have a select box in my View. What I want to do is, when I select an option in the select box, it will update the view with data fetched from database. Below is a snippet of my attempted codes:
View.php
< script >
$('#examId').on('change', function() {
var optionSelected = $(this).find("option:selected");
var examid = optionSelected.val();
alert(examid);
$.ajax({
type: "post",
url: "/admin/testresults/show",
data: {
id: examid
}
// data: $("#examId").val()
})
.done(function() {
alert('im here');
});
}); < /script>
<form name="form1" method="post" action="testresults">
<select name="examId" style="width:50%;" id="examId">
<option value='non'>Select an exam...</option>
<option value='1'>Foo1</option>
<option value='2'>Foo2</option>
</select>
<input type="submit" />
</form>
if(isset($rows)){
$i=1;
foreach ($rows as $row) {
print "
<tr>
<td>".$i."</td>
<td>".$row->name." ".$row->last_name."</td>
<td>".($result = ($row->result == 1) ? 'Pass' : 'Fail')."</td>
</tr>
";
$i++;
}
}
Controller.php
public function showTestResults(){
$examId = Input::get('id');
$rows = TestResults::getExamNamebyID($examId);
return View::make('backend.admin.testResults.index')->with('rows', $rows);
}
Route.php
Route::post('testresults/show',array('as' => 'show','uses' => 'AdminController@showTestResults'));
Model.php
public static function getExamResults($examId){
return DB::table('testresults')
->join('users', 'users.id', '=', 'testresults.userId')
->where('examId', $examId)
->groupBy('testresults.userId')
->get();
}
However, when i choose an option in the select box, I'm getting the following error:
POST http://localhost:8000/admin/testresults/show 500 (Internal Server Error)
What should I change? Is my concept correct? Thanks!
Upvotes: 0
Views: 7462
Reputation: 21901
seems like your routing is not correct, so please try this one,
change your view.php
in to a blade file, to do that rename the view.php
into view.blade.php
in your view
file,
replace
<form name="form1" method="post" action="testresults">
with
Form::open(array('route' => 'show', 'name' => 'form1', 'id' => 'testForm', 'method' => 'POST'))
or
<form name="form1" method="post" action="{{ URL::route('show') }}" id="testForm">
in your js
,
....
$.ajax({
type: "post",
url: $("#testForm").attr('action'),
data: {
id: examid
}
// data: $("#examId").val()
})
.done(function() {
alert('im here');
});
....
Upvotes: 2