0

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!

8
  • is your route correct ? do u hit on the showTestResults function ? Commented Jan 30, 2015 at 7:13
  • @K.Toress can u guide me on how can I check that? Commented Jan 30, 2015 at 7:15
  • just return something in that controller like return "success"; and check you have get the same error or not ? dont forget to comment other things in the function. :) Commented Jan 30, 2015 at 7:16
  • @K.Toress i tried to change the controller to return a view called hello...did not work Commented Jan 30, 2015 at 7:22
  • so the problem is with your routes please check the routes Commented Jan 30, 2015 at 7:56

1 Answer 1

2

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');
});
....
Sign up to request clarification or add additional context in comments.

1 Comment

doesn't work, it says link_to_route() does not exist

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.