Created
December 30, 2022 10:54
-
-
Save faizalanwar/5b4c8da3c70b85fe438b6b5e269838f0 to your computer and use it in GitHub Desktop.
ajax filter select laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public function getdesa(Request $request) | |
| { | |
| // dd($request->all()); | |
| // dd($request->kecamatan_id); | |
| $desa = desa::where('kecamatan_id', $request->kecamatan_id)->get(); | |
| // dd($desa); | |
| echo "<option selected disabled>Please select one option</option>"; | |
| foreach ($desa as $data) { | |
| echo "<option value=$data->desa_id>$data->nama_desa</option>"; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div class="center row col-md-12 g-2 mt-3 " id="kec"> | |
| <div class="col-md-6"> | |
| <div class="form-group row"> | |
| <label class="col-md-12 label-control" for="kecamatan">Pilih | |
| Kecamatan</label> | |
| <div class="col-md-12"> | |
| <select class="select2 form-control block kecamatan_id" name="kecamatan_id" id="kecamatan_id" | |
| autocomplete="off"> | |
| <option selected disabled>Please select one option</option> | |
| @foreach ($kecamatan as $i => $k) | |
| <option value="{{ $k->kecamatan_id }}"> | |
| {{ $i + 1 }}. {{ $k->nama_kecamatan }}</option> | |
| @endforeach | |
| </select> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <div class="center row col-md-12 g-2 mt-3 " id="des" > | |
| <div class="col-md-6"> | |
| <div class="form-group row"> | |
| <label class="col-md-12 label-control" for="desa">Pilih | |
| desa</label> | |
| <div class="col-md-12"> | |
| <select class="select2 form-control block desa_id" name="desa_id" id="desa_id" autocomplete="off"> | |
| <option selected disabled>Please select one option</option> | |
| @foreach ($desa as $i => $k) | |
| <option value="{{ $k->desa_id }}"> | |
| {{ $i + 1 }}. {{ $k->nama_desa }}</option> | |
| @endforeach | |
| </select> | |
| </div> | |
| </div> | |
| </div> | |
| </div> | |
| <script> | |
| $(document).ready(function() { | |
| $('#kecamatan_id').change(function() { | |
| var kecamatan_id = $(this).val(); | |
| var _token = $('input[name="_token"]').val(); | |
| $.ajax({ | |
| url: "{{ route('form.getdesa') }}", | |
| method: "POST", | |
| data: { | |
| kecamatan_id: kecamatan_id, | |
| _token: _token, | |
| }, | |
| success: function(result) { | |
| $('#desa_id').html(result); | |
| } | |
| }) | |
| }); | |
| }); | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Route::post('/form/getdesa', [FormController::class, 'getdesa'])->name('form.getdesa'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment