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

jeetdholakia's avatar

How to get dynamic data in bootstrap modal?

Hello, I am developing an app which requires options like view, edit and delete details. So for viewing details I am using bootstrap modal to show the details. The problem is how to display data in to bootstrap modal according to id. Thanks in advance.

0 likes
52 replies
tomopongrac's avatar
  1. Get id with jQuery
  2. Pass it to controller via Ajax
  3. Get your result back
2 likes
jeetdholakia's avatar

@tomo_pongrac I am displaying table using for loop to display 4-5 fields from all the fields. When user clicks on "View All Details" modal should open and it should display all the data specific to that ID. Code:

<table class="table no-margin text-center">
          <thead>
            <tr>
              <th>Society ID</th>
              <th>Society Name</th>
              <th>Address</th>
              <th>Secretary Name</th>
              <th>Actions</th>
            </tr>
          </thead>
          <tbody>
            @foreach($societydetails as $sd)
            <tr>
              <td>{{ $sd->societyid }}</td>
              <td>{{ $sd->societyname }}</td>
              <td>{{ $sd->address }}</td>
              <td>{{ $sd->secretaryname }}</td>
              <td><button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target="#societydetails">View All Details</button></td>
            </tr>
            @endforeach
          </tbody>
        </table>    

And basic modal code to display data

<div class="modal-body">
            <!-- <p>One fine body&hellip;</p> -->
            <table class="table table-striped">
              <tr>
                <td>Society ID</td>
                <td >Example</td>
              </tr>
              <tr>
                <td>Society Name</td>
                <td >Example</td>
              </tr>
        .... some other data also 
            </table>
          </div><!-- modal body -->
cklmercer's avatar

Looks like a good use case for vuejs/vue

3 likes
tomopongrac's avatar

@jeetdholakia

Try this

data-target-id - id from database

"/controller/ " + id ... you controller which returns html controller must return view. View is what is inside the .modal-body and controller must return in this format

$returnHTML = view('modal-body-view',[' variable_name'=> $variable_name])->render();
return response()->json( 'html'=>$returnHTML);

I hope that will be work ... i did't tested

<script>
    $(document).ready(function(){
        $("#myModal").on("show.bs.modal", function(e) {
            var id = $(e.relatedTarget).data('target-id');
            $.get( "/controller/" + id, function( data ) {
                $(".modal-body").html(data.html);
            });

        });
    });
</script>


your view
<table class="table no-margin text-center">
    <thead>
    <tr>
        <th>Society ID</th>
        <th>Society Name</th>
        <th>Address</th>
        <th>Secretary Name</th>
        <th>Actions</th>
    </tr>
    </thead>
    <tbody>
    @foreach($societydetails as $sd)
    <tr>
        <td>{{ $sd->societyid }}</td>
        <td>{{ $sd->societyname }}</td>
        <td>{{ $sd->address }}</td>
        <td>{{ $sd->secretaryname }}</td>
        <td><button type="button" class="btn btn-success btn-sm" data-toggle="modal" data-target-id="1" data-target="#myModal">View All Details</button></td>
    </tr>
    @endforeach
    </tbody>
</table>

<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
                <h4 class="modal-title" id="myModalLabel">Modal title</h4>
            </div>

            <div class="modal-body">

            </div>


        </div>
    </div>
</div>
pmall's avatar

How would you dynamically change the content of a html div? Wel, it is exactly the same with a modal, you just have to open it at the end.

tomopongrac's avatar

@jeetdholakia

you need to create normal controller with the information that you want to display only with the difference that you return json format ...

tomopongrac's avatar

@jeetdholakia

Try this approach

$view = View::make('author_view', ['name' => 'Arjun']);
$contents = $view->render();
// or
$contents = (string) $view;

but then in javascript you must write

$(".modal-body").html(data);
jeetdholakia's avatar

@tomo_pongrac That worked but can u tell me what to write in

$.get( "/controller/" + id, function( data ) 

This "/controller/" url?

jeetdholakia's avatar

@tomo_pongrac This is proper?

$.get( {{ route('viewmodal',$sd->societyid) }}, function( data )

Because in modal nothing is coming.

tomopongrac's avatar

@jeetdholakia

No, because that is javaScript so you can use laravel functions. You must manually write link without id because id is generated by javaScript

jeetdholakia's avatar

@tomo_pongrac Ok I added the link manually but still the modal body is empty.

<script>
    $(document).ready(function(){
      var base_url = window.location.origin;
      $("#societydetails").on("show.bs.modal", function(e) {
        var id = $(e.relatedTarget).data('target-id');
        $.get(base_url + '/admin' + '/society/' + id, function( data ) {
          $(".modal-body").html(data);
        });

      });
    });
  </script>
tomopongrac's avatar

@jeetdholakia

Try this

<script>
    $(document).ready(function(){
      var base_url = window.location.origin;
      $("#societydetails").on("show.bs.modal", function(e) {
        var id = $(e.relatedTarget).data('target-id');
        $.get('/admin/society/' + id, function( data ) {
          $(".modal-body").html(data);
        });

      });
    });
  </script>

And you can open console and look if shows some errors https://developer.chrome.com/devtools/docs/console

tomopongrac's avatar

@jeetdholakia

Can you post link for ajax call which you can see in console

I think that you have php error in controller...

jeetdholakia's avatar

@tomo_pongrac Here is the code for controller:

public function viewmodal($societyid)
    {
        $view = View::make('admin.modalview', ['societyid'=> $societyid]);
        $contents = $view->render();
    }   

and here is the error in console: http://prntscr.com/bcymjz

tomopongrac's avatar

@ jeetdholakia

Does the "KPCP" is valid societyid?

in view you have attribute data-target-id="societyid"

tomopongrac's avatar

And you have route

Route::get('admin/society/{id}', 'Controller@viewmodal');

You can try for test open link localhost:8000/admin/society/KPCP

where you modigy controller

public function viewmodal($societyid)
    {
    return view('admin.modalview', ['societyid'=> $societyid]);
    }   
jeetdholakia's avatar

@tomo_pongrac If I open localhost:8000/admin/society/KPCP it gives me blank page with nothing on it. The routes and controller are exact same like you have mentioned.

tomopongrac's avatar

@jeetdholakia

then it looks like view is not correct

Also, now i see that you must create $sd object in controller something like this (where Society is name of you model)

public function viewmodal($societyid)
    {
    $sd = Society::find(societyid);
    return view('admin.modalview', ['sd'=> $sd]);
    }  
jeetdholakia's avatar

@tomo_pongrac I changed the controller code according to the above mentioned code by you, still the modal shows no data and I am getting following internal server error: http://prntscr.com/bcyzxf modalview.blade.php:

<table class="table table-striped">
  <tr>
    <td>Society ID</td>
    <td></td>
  </tr>
  <tr>
    <td>Society Name</td>
    <td></td>
  </tr>
</table>    

PS: I am sorry for asking this much and thanks a lot for helping me.

Next

Please or to participate in this conversation.