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

my_name_is_dimitris's avatar

add modal popup from ajax fetched results

im struggling to achieve this because my javascript knowledge is not that good.

So what im trying to do is this: I'm building a borrowing book laravel app and i have a landing page that has a search box and a search button. When someone starts typing a query an auto-complete suggestion is triggered and a drop-down of book titles is available to choose from. The problem is that i cant use the search result to show any book details in a popup modal.

route:

Route::get('autocomplete', 'App\Http\Controllers\WelcomeController@search');

method in my controller:


public function search(Request $request)
    {
          $search = $request->get('term');

          $result = Book::where('title', 'LIKE', '%'. $search. '%')->get();


          return response()->json($result);

    }

_searchbox.blade.php :

   <div class="col-md-10 col-lg-8 col-xl-7 mx-auto">
                    <form method="">
                        <div class="form-row">
                            <div class="col-12 col-md-9 mb-2 mb-md-0">
                                <input id="search"  "type="text" class="form-control form-control-lg" name="search"
                                    placeholder="Search for...">

                            </div>

                            <div class="col-12 col-md-3">
                                <button type="button" name="searchButton" id="searchButton" class="btn btn-block btn-lg btn-primary" ><i class="fa fa-search"></i></button>
                            </div>
                        </div>
                    </form>
                </div>

script for fetching the results :


$(document).ready(function() {
       $( "#search" ).autocomplete({

           source: function(request, response) {
               $.ajax({
               url: "{{url('autocomplete')}}",
               data: {
                       term : request.term
                },
               dataType: "json",
               success: function(data){
                  var resp = $.map(data,function(obj){
                      // console.log(obj.city_name);
                       return obj.title;
                  });

                  response(resp);
               }
           });
       },
       minLength: 1
    });

i followed this tutorial : https://w3path.com/jquery-ui-autocomplete-search-in-laravel-6-tutorial/

Can someone help me with that?

0 likes
3 replies
Charizard's avatar

can you give more details into what the problem is? is the dropdown not showing or are you receiving any errors on the console?

my_name_is_dimitris's avatar

Dropdown is working just fine. What i want is to press the search button and show a modal which shows the requested book details.

$(document).on('click', '#searchButton', function (event) {event.preventDefault();
            let href = $(this).attr('data-attr');
            $.ajax({
                url:href,

                beforeSend: function () {
                    $('#loader').show();
                },
                // return the result
                success: function (result) {
                    $('#mediumModal').modal("show");
                    $('#mediumBody').html(result).show();
                },
                complete: function () {
                    $('#loader').hide();
                }

I dont see any problems in the console. The problem is that i cant combine the fetched result of the dropdown and pass it to the modal

my_name_is_dimitris's avatar

A hardcoded example of what i am trying to achieve is this

$(document).on('click', '#searchButton', function (event) {event.preventDefault();
            let href = $(this).attr('data-attr');
            $.ajax({
                url:"/books/12",

                beforeSend: function () {
                    $('#loader').show();
                },
                // return the result
                success: function (result) {
                    $('#mediumModal').modal("show");
                    $('#mediumBody').html(result).show();
                },
                complete: function () {
                    $('#loader').hide();
                },
                error: function (jqXHR, testStatus, error) {
                    console.log(error);
                    alert("Page " + href + " cannot open. Error:" + error);
                    $('#loader').hide();
                },
                timeout: 8000
            })
        });

while pressing the search button ,a modal with the details of a book with an id of 12 is shown. What i am trying to do is pass the value from the auto-complete search bar to the search button and show the details of a book in a modal. Any help is much appreciated,thanks.

Please or to participate in this conversation.