1

I want to passing a variable (id) from view to controller using javascript onChange. I have done it but the result when I'm do dd always the same. In my case i always get "7".

Here is my code:

View:

 <select id="jurusan_id" type="text" name="jurusan_id" onchange="filterJurusan()">
       <option value disable>Jurusan</option>
         @foreach (App\Models\Admin\Jurusan::all() as $jurusan)
          <option value="{{ $jurusan->id }}">{{ $jurusan->nama_jurusan }}</option>
         @endforeach
    </select>

Route:

 Route::get('datasiswa/{id}', 'Admin\SiswaController@filterSiswa')->name('filterSiswa');

Js:

function filterJurusan() {
  window.location.href = "{{ route('filterSiswa', $jurusan->id) }}";
}

Controller:

public function filterSiswa(Request $request, $id)
  {
      dd($id);
  }

I know something was wrong with my code, but i can't figure out what it is.

3 Answers 3

1

I think you are confusing server-side and client-side code. The JS code piece renders the route once upon loading. The $jurusan variable is set to the last object of your foreach loop. Therefore the id will always be 7.

You will have to get the selected item. For that adjust the onchange function:

onchange="filterJurusan(this)"

Afterwards you can get the value, which is the id like this.

function filterJurusan(el) {
 var value = (el.value || el.options[el.selectedIndex].value); 
  window.location.href = '/datasiswa/'+value;
}

Now the question is if you simply want to change the location (like in the code) or as your function names indicate, want to filter data?

Sign up to request clarification or add additional context in comments.

4 Comments

I want to filter data using select. So i need an id as a parameter. Btw this code working
if you want to stay on the same page you will have to make some ajax requests :)
Thanks for information, then I'll do some research about using ajax for filtering data
@Catto I don't know much you want to learn (or read about) but a good choice would be to use Vue.js (this will make displaying dynamic data very easy)
1

Actually, you are populating Jurusan records on view by App\Models\Admin\Jurusan::all() as $jurusan. What this does is populate options in your select but at the end of iterations last record's data left stored in $jurusan variable that you are using in function filterJurusan() to call controller method. You should get jurusanId = document.getElementById('jurusan_id') and pass it to the controller like window.location.href = '/datasiswa/' + jurusanId;

1 Comment

You should return all records from the controller method using for this view. That's how you will achieve actual MVC.
0

This can be more easier and accurate to get your result..

<form action="{{route('filterSiswa')}}" method="GET">
 <select onchange="this.form.submit()" name="jurusan_id">
   <option value disable>Jurusan</option>
     @foreach (App\Models\Admin\Jurusan::all() as $jurusan)
      <option value="{{ $jurusan->id }}">{{ $jurusan->nama_jurusan }}</option>
     @endforeach
 </select>
 </form>

2 Comments

Thanks, but using this method i can't get id from foreach
Oh yes, I forget to mention that, you have to update your route, Route::get('datasiswa', 'Admin\SiswaController@filterSiswa')->name('filterSiswa'); And catch id in controller as follow: request()->id

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.