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!
showTestResultsfunction ?return "success";and check you have get the same error or not ? dont forget to comment other things in the function. :)