Home > Blockchain >  Fetch data from database using jquery via selectbox in Laravel Project
Fetch data from database using jquery via selectbox in Laravel Project

Time:06-29

I am trying to load information such as 'name' from a table where the admission number matches the one selected from the select box using jquery. With the code below, it only select the first 'name' from the table in the database and it doesn't select with respect to the admission number on the select box. (Note: I am not very familiar with jquery)

Code for the Route.

 Route::post('get-student-info/', [ResultController::class, 'get_student_info']);

Controller code

public function get_student_info(Request $request)
{
    $adnum=$request->post('admission_num');
    $stud=User::where('admission_num',$adnum)->get();

    foreach ($stud as $list)

    {


            $html='<p><b>First Name :'.$list->name.'</b></p>';

    }

    echo $html;


}

Blade.php code

<div >
   <select  name="admission_num" id="admission_num" aria-label="Default select example">
                       <option></option>
                           @foreach($admission as $admissions)
                       <option value="{{ $admissions->admission_num }}">{{ $admissions->admission_num }}</option>
                           @endforeach
                           </select>
                                               
       </div>

Output shows here

<div id="info" >
                                                            <p><b>First Name :</b></p>
                                                            <p><b>Last Name :</b></p>
                                                           
                                                        </div>

JQuery Code

<script>
                        jQuery(document).ready(function(){
                        jQuery('#admission_num').change(function(){
                            let adnum=jQuery(this).val();
                            jQuery('#info').html('<p><b>First Name :</b></p>')
                            jQuery.ajax({
                                url:'/get-student-info',
                                type:'post',
                                data:'adnum=' adnum '&_token={{csrf_token()}}',
                                success:function(result){
                                    jQuery('#info').html(result)
                                }
                            });
                        });

                    });
                    </script>

CodePudding user response:

Seems like no issue in jquery part. I see the issue is in the Controller.

After you are getting relevant results using admission number and loop it for a output. but in the loop it always give us to the last record (according to your code)

try this way.

public function get_student_info(Request $request)
    {
        $adnum=$request->post('admission_num');
        $stud=User::where('admission_num',$adnum)->get();
    
        $html=''; //before looping the result initialize the output variable

        foreach ($stud as $list)
        {
    
               $html ='<p><b>First Name :'.$list->name.'</b></p>'; // use = to  = this will bind the result
               $html ='<p><b>First Name :'.$list->last_name.'</b></p>'; // if you want last name, assume user table column has 'last_name'
        }
    
        echo $html;
}

CodePudding user response:

  • you can return view in your method and Ajax query you must set dataType = "html"
  • EX:
$.ajax({
        url: `/products/getAjax`,
        'method': 'GET',
        'dataType': 'html',
    }).done(function (data) {
        setLoading(false);
        $('#product-wrapper').html(data);
    }).fail(function (data) {
        console.log("           
  • Related