PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, December 29, 2021

[FIXED] $request->input Returning Null in Laravel

 December 29, 2021     ajax, jquery, laravel, laravel-8     No comments   

Issue

I want to try if i select a value that is in the "dropdown" it will automatically replace another value in the same page without updating the page, and using ajax, but on the controller part i get the message "request=null" i don't understand even though I have entered the name of the input from "view" to the controller. this is some of my project code

view.blade

<label class="col-sm-2 form-inline">Karyawan</label>
<select class="form-select select-name" id="idSelect" aria-label="Default select example" name="pilih-nama">
  <option value="">-Pilih Karyawan-</option>
   @foreach ($karyawan as $data)
      <option value=" {{$data->nip_kyn}}">{{$data->nama_kyn}}</option>
   @endforeach
</select>

Controller

public function showData(Request $request)
{
    $nip = $request->input('pilih-nama');
    $data = Karyawan::where('nip_kyn', $nip)->first();
    return response()->json(['data' => $data]);
}

Ajax code in main view

    <script>
    $(document).on('change', '.select-name', function() {
        $.ajaxSetup({
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        });
        var hasil = $(this).val();
        // 
        if (hasil == '') {
            document.location.href = "unduhQR";
        }
        $.ajax({
            url: "{{route('showData.index')}}",
            type: 'post',
            data: {
                data: hasil
            },
            success: function(res) {
                console.log(res.data)
                // $('.nama-kar').html(res.data.nama_kyn)
                // $('.idQr').html('NIP : ' + res.data.nip_kyn)
            },
            error: function(xhr) {
                // console.log(xhr)
            }
        })
    })
</script>

my route

Route::post('/showQR', [QRCodeController::class, 'showData'])->name('showData.index');

but if i change in my controller like this

    $nip = 'NIP001'; // <-- Value from database
    $data = Karyawan::where('nip_kyn', $nip)->first();
    return response()->json(['data' => $data]);

its going work, but that's not dynamic, any suggestion to solve this guys ?


Solution

You need to send the data like this

data: {pilih-nama: hasil}

and to be honest your variable names are not good. You should change them with good ones.



Answered By - rergun
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing