skip to Main Content

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 class="col-md-3 ms-3">
   <select class="form-select form-select-sm" 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" class="receipt-right">
                                                            <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>

2

Answers


  1. 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;
    }
    
    Login or Signup to reply.
    • 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("🚀 ~ file: list.js ~ line 10 ~ data", data)
        });
    
    • Method
    public function getAllAjax(Request $request)
        {
            $products = $this->productReponsitory->getAllPProduct($page);
            return view('users.pages.products.list.parts.product-show', compact('products'));
    
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search