I want to replace the input value using ajax in laravel

  ajax, jquery, laravel, php

I want to get the student award date and replace the value in the existing input but I write some jquery it is not working place help to fix this issue. I attached my code down.

Controller

public function show(Request $request)
{
if($request->studentId!='')
        {
            $html = "";
            $StudentCDate = Student::where('id', $request->student_id)->first();

            $data = $StudentCDate->stud_completion_date;

            $html .= $data;

            //$html .=".$StudentCDate->stud_completion_date.";

        }

        return response()->json(['html' => $html]);
}

Jquery Script

<script type="text/javascript">
        $(document).ready(function(){
            // Department Change
            $('#student_id').change(function(){
                // Department id
                var id = $(this).val();
                //alert(id);
                // Empty the dropdown
                //$('#date').find('input').not(':first').remove();
                // AJAX request
                $.ajax({
                    url: "{{ route('StudentCertificateCount/show') }}?studentId=" + id,
                    type: 'get',
                    dataType: 'json',
                    success: function(response){
                        alert(response.html);
                        //$('#date').val(response.html);
                    }
                });
            });
        });
    </script>

My Input

<div class="mt-3">
  <label class="flex flex-col sm:flex-row"> Awarded Date </label>
  <input type="text" id="date" name="awarded_date" class="input w-full border mt-2" 
  placeholder="dd/mm/yyyy">
</div>

Source: Ask PHP

LEAVE A COMMENT