Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

faizaltkl's avatar

how to do ajax in laravel

Hi,

Good Morning...

I have drop down and two text box in my form.When selecting value from dropdown , i have to load the corresponding information from mysql table of selected dropdown value to textbox(e.g : Name,address of selected dropdown value).I have to do using ajax.Please tell me how to do ajax in laravel 5

 Thanks in advance...
0 likes
39 replies
Reached's avatar

Hi @khader ,

AJAX in Laravel is no different from AJAX in other frameworks.

Do you have some code that shows what you have tried so far?

1 like
clay's avatar

Many ways to handle this but the idea is to make an Ajax request using your preferred JavaScript library, eg. using jquery

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

Be sure to have a route set up to handle it in your routes file. Then in a controller method, do the logic to get what you need and return the data like

public function getData($id) {
  $data = Person::find($id);
  return->response()->json(['status' => 'success', 'data' => $person], 200);
}

Then use that data in the 'success' part of your Ajax function to populate the form or whatever.

Hope that helps.

1 like
faizaltkl's avatar

hi

the following is my code..

1.javascript methid in view:

<script>
function  function_imei(){

 var selected_imei = document.getElementById('selectImei').value;

  $.ajax({
      url: '/info/' + selected_imei,
      type: 'get',
      data: {},
      success: function(data) {
       if (data.success == true) {
         $("#iname").value = data.info;
         alert("sucecess");
       } else {
         alert('Cannot find info');
       }
   }
   });
}
</script>

2.Controller:

  public function getInfo($id)
     {
       $fill = DB::table('register_devices')->where('imei_number', $id)->get();

       return Response::json(['success'=>true, 'info'=>$fill]);
     }

3:route:

Route::get('infos/{id}', 'GpsPageController@getInfo');

when selecting dropdown i am not getting any response ...please help me to solve issue.....
faizaltkl's avatar

Hi

   @tomo_pongrac , 

         Thank u for u r help.  I changed 
 url: '/info/' + selected_imei, to  url: '/infos/' + selected_imei, 

Still i am not getting any response.. I think my ajax method is not correct... can u help anyone..

tomopongrac's avatar

@khader

did you bind event handler

https://api.jquery.com/change/

$('#selectImei').change(function(){

    var selected_imei = document.getElementById('selectImei').value;

   // You can perform an ajax request using the .ajax() method
   $.ajax({
       type: 'GET',
     url: '/info/' + selected_imei,



      success: function(data) {
       if (data.success == true) {
         $("#iname").value = data.info;
         alert("sucecess");
       } else {
         alert('Cannot find info');
       }
   }
    });

});
faizaltkl's avatar

Hi,

@tomo_pongrac

yes.I binded.The following is select form..

 <div class="control-group">                               
                                          <select id="selectImei" name="opttest" onchange="function_imei()">
                                               <option value="select imei number">select imei number</option>
                                               @foreach($imei_List as $user_im)                                                                      
                                               <option name="optvalue"> {{$user_im->imei_number}} </option>
                                               @endforeach               
                                            </select>       
                                         </div> 
petrit's avatar

You have to define what variable to sent to controller

...
$.ajax({
      url: '/info/' + selected_imei,
      type: 'get',
      data: {
        id: $('#id').val()
        },
...
faizaltkl's avatar

@tomo_pongrac

                                 <select id="selectImei" name="opttest">
                                               <option value="select imei number">select imei number</option>
                                               @foreach($imei_List as $user_im)                                                                      
                                       <option name="optvalue" value="{{$user_im->imei_number}}"> {{$user_im->imei_number}} </option>
                                               @endforeach               
                                 </select>  

can u tell me what is wrong with select...please

tomopongrac's avatar

Remove name from code bellow

<option value="{{$user_im->imei_number}}"> {{$user_im->imei_number}} </option>
faizaltkl's avatar

@tomo_pongrac

yah. I removed name.. but still not getting any response . I tested with alert box , im getting alert inside method.

please help to solve this issue

tomopongrac's avatar

Which alert exactly did you get ... did you try with

alert(data);
tomopongrac's avatar

Open console a see errors

Did you load jquery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>

and try this code

<script>
    function  function_imei(){
        alert("function called");
        var selected_imei = document.getElementById('selectImei').value;
        alert("Value:" + selected_imei;

        $.ajax({
            url: '/infos/' + selected_imei,
            type: 'get',
            data: {},
            success: function(data) {
                alert(data);
            }
        });
    }
</script>

faizaltkl's avatar

@tomo_pongrac

I applied u r code , i m getting only first two alert...

I think in route have problem ..please verify

my route is:

Route::get('getInfo{id}', 'GpsPageController@getInfo');


Controller:

public function getInfo($id)
     {
       $fill = DB::table('register_devices')->where('imei_number', $id)->get();

       return Response::json(['success'=>true, 'info'=>$fill]);
     }


view (ajax):

<script>
    function  function_imei(){
        alert("function called");
        var selected_imei = document.getElementById('selectImei').value;
        alert("Value:" + selected_imei);

        $.ajax({
            url: 'getInfo/' + selected_imei,
            type: 'get',
            data: {},
            success: function(data) {
                alert(data);
            }
        });
    }
</script>


tomopongrac's avatar

Route must be

Route::get('getInfo/{id}', 'GpsPageController@getInfo');
tomopongrac's avatar

You local server dont working ... can you load home page of that laravel project and which link is that

tomopongrac's avatar

Add this to link in ajax call

url: '/laravel/GpsTrack/public/getInfo/' + selected_imei,
faizaltkl's avatar

@tomo_pongrac

Thanks...

Now url is ok ..

In   alert(data)   i am getting  object Object ...  how i can get actual data

faizaltkl's avatar

Ajax code :

 $.ajax({
            url: '/laravel/GpsTrack/public/getInfo/' + selected_imei,
            type: 'get',
            data: {},
            success: function(data) {            
                alert(data.info.imei_number);
            }
        });

1: I am getting alert as undefined

case 2: alert(data.info); and alert(data) - I am getting object Object

Controller:

public function getInfo($id)
     {
      $fill = DB::table('register_devices')->where('imei_number', $id)->get();
       
       return Response::json(['success'=>true, 'info'=>$fill]);
     }
tomopongrac's avatar

instead alert put this command and look at console

console.log(data.info);

Next

Please or to participate in this conversation.