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

Tchopa's avatar

Same note is displayed everytime, whilst ID is changing

Hi all,

I'm using Ajax to open and close modals to display note data to users. Currently, everything works EXCEPT for when you click to display the note, as it only shows the note with id->1, regardless of which note you click in the foreach loop.

See below for code thats used to make this work. Any help would be appreciated, thanks.

Controller

public function noteDisplay(Client $client)
   {
      $data['notes'] = Note::where('client_id', $client->id)->get();
      return view('database.clients.test.modal',$data);
   }

   public function getNoteDetails($note){

      $note = Note::find($note);

      $html = "";
      if(!empty($note)){
         $html = "<tr>
              <td width='30%'><b>Created At:</b></td>
              <td width='70%'> ".$note->created_at."</td>
           </tr>
           <tr>
              <td width='30%'><b>Title:</b></td>
              <td width='70%'> ".$note->title."</td>
           </tr>
           <tr>
              <td width='30%'><b>Category:</b></td>
              <td width='70%'> ".$note->category."</td>
           </tr>
           <tr>
              <td width='30%'><b>Description:</b></td>
              <td width='70%'> ".$note->description."</td>
           </tr>";
      }
      $response['html'] = $html;

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

View

<table class="table filter-table" id='noteTable'>
         <thead>
            <tr>
               <th>S.no</th>
               <th>Title</th>
               <th>Category</th>
               <th>Description</th>
               <th>&nbsp;</th>
            </tr>
         </thead>
         <tbody>
         @php
         $sno = 0;
         @endphp
         @foreach($notes as $note)
            <tr>
              <td>{{ $note->id }}</td>
              <td>{{ $note->title }}</td>
              <td>{{ $note->category }}</td>
              <td>{{ $note->description }}</td>
              <td><button class='btn btn-info viewdetails' data-id='{{ $note->id }}' >View Details</button></td>
            </tr>
         @endforeach
         </tbody>
      </table>

   </div>
   
   
   <!-- Modal -->
      <div class="modal fade" id="noteModal" >
         <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
               <div class="modal-header">
                  <h4 class="modal-title">Note Info</h4>
                  <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
               </div>
               <div class="modal-body">
                   <table class="w-100" id="tblnoteinfo">
                      <tbody></tbody>
                   </table>
               </div>
               <div class="modal-footer">
                   <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
               </div>
            </div>
         </div>
      </div>

   <!-- Script -->
   <script type='text/javascript'>
   $(document).ready(function(){

      $('#noteTable').on('click','.viewdetails',function(){
          var note = $(this).attr('data-id');

          if(note > 0){

             // AJAX request
             var url = "{{ route('getNoteDetails', ['note' => $note->id, 'client' => $note->client_id]) }}";
             url = url.replace(':note',note);

             // Empty modal data
             $('#tblnoteinfo tbody').empty();

             $.ajax({
                 url: url,
                 dataType: 'json',
                 success: function(response){

                     // Add note details
                     $('#tblnoteinfo tbody').html(response.html);

                     // Display Modal
                     $('#noteModal').modal('show'); 
                 }
             });
          }
      });

   });
   </script>

Routes (web.php)

Route::get('/database/clients/{client}/notes', [App\Http\Controllers\ClientController::class, 'noteDisplay'])->name('database.clients.notes.show');
Route::get('/database/clients/{client}/notes/{note}', [App\Http\Controllers\ClientController::class, 'getNoteDetails'])->name('getNoteDetails');
0 likes
8 replies
Nakov's avatar

I am not sure you need this line:

 url = url.replace(':note',note);

when you inspect your "Network" tab in the browsers Developer Console, what do you see? The request contains the right data?

Basically this one is wrong:

var url = "{{ route('getNoteDetails', ['note' => $note->id, 'client' => $note->client_id]) }}";

Instead what I would do is change this:

<button class='btn btn-info viewdetails' data-id='{{ $note->id }}' >View Details</button>

to

<button class='btn btn-info viewdetails' data-url="{{ route('getNoteDetails', ['note' => $note->id, 'client' => $note->client_id]) }}" >View Details</button>

and then in JS just

var url = $(this).attr('data-url');

 if(url){
... // perform the AJAX request with the given URL.
2 likes
Tchopa's avatar

@Nakov

Hi Nakov, thanks for suggesting those changes. I made the suggested changes and when I click View Details nothing happens. See below for JS with edits.

<!-- Script -->
   <script type='text/javascript'>
   $(document).ready(function(){

      $('#noteTable').on('click','.viewdetails',function(){
          var note = $(this).attr('data-id');

          if(note > 0){

             // AJAX request
             var url = $(this).attr('data-url');
             
             if (url){

             // Empty modal data
             $('#tblnoteinfo tbody').empty();

             $.ajax({
                 url: url,
                 dataType: 'json',
                 success: function(response){

                     // Add note details
                     $('#tblnoteinfo tbody').html(response.html);

                     // Display Modal
                     $('#noteModal').modal('show'); 
                 }
             });
          }
      });

   });
   </script>
Nakov's avatar

@Tchopa I was suggesting that you replace this:

var note = $(this).attr('data-id');

if(note > 0){

with this:

var url = $(this).attr('data-url');
             
if (url){

and of course on the button instead of data-id to add data-url with my suggestion in the previous answer.

1 like
Tchopa's avatar

@Nakov

Hi Nakov,

I made those changes, and the View Details will open up the modal, but the data displayed in the modal is still the same data everytime, it is not changing based on the note->id that I select. See below code with your suggested changes.

<table class="table filter-table" id='noteTable'>
         <thead>
            <tr>
               <th>S.no</th>
               <th>Title</th>
               <th>Category</th>
               <th>Description</th>
               <th>&nbsp;</th>
            </tr>
         </thead>
         <tbody>
         @php
         $sno = 0;
         @endphp
         @foreach($notes as $note)
            <tr>
              <td>{{ $note->id }}</td>
              <td>{{ $note->title }}</td>
              <td>{{ $note->category }}</td>
              <td>{{ $note->description }}</td>
              <td>
                  <button class='btn btn-info viewdetails' data-url="{{ route('getNoteDetails', ['note' => $note->id, 'client' => $note->client_id]) }}" >View Details</button>

                </td>
            </tr>
         @endforeach
         </tbody>
      </table>

   </div>
   
   
   <!-- Modal -->
      <div class="modal fade" id="noteModal" >
         <div class="modal-dialog">

            <!-- Modal content-->
            <div class="modal-content">
               <div class="modal-header">
                  <h4 class="modal-title">Note Info</h4>
                  <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
               </div>
               <div class="modal-body">
                   <table class="w-100" id="tblnoteinfo">
                      <tbody></tbody>
                   </table>
               </div>
               <div class="modal-footer">
                   <button type="button" class="btn btn-danger" data-bs-dismiss="modal">Close</button>
               </div>
            </div>
         </div>
      </div>

   <!-- Script -->
   <script type='text/javascript'>
   $(document).ready(function(){

      $('#noteTable').on('click','.viewdetails',function(){
          var url = $(this).attr('data-url');
             
            if (url){

             // Empty modal data
             $('#tblnoteinfo tbody').empty();

             $.ajax({
                 url: url,
                 dataType: 'json',
                 success: function(response){

                     // Add note details
                     $('#tblnoteinfo tbody').html(response.html);

                     // Display Modal
                     $('#noteModal').modal('show'); 
                 }
             });
          }
      });

   });
   </script>
Nakov's avatar
Nakov
Best Answer
Level 73

@Tchopa can you change your method in the controller:

public function getNoteDetails(Client $client, Note $note){
         $html = "<tr>
              <td width='30%'><b>Created At:</b></td>
              <td width='70%'> ".$note->created_at."</td>
           </tr>
           <tr>
              <td width='30%'><b>Title:</b></td>
              <td width='70%'> ".$note->title."</td>
           </tr>
           <tr>
              <td width='30%'><b>Category:</b></td>
              <td width='70%'> ".$note->category."</td>
           </tr>
           <tr>
              <td width='30%'><b>Description:</b></td>
              <td width='70%'> ".$note->description."</td>
           </tr>";
      $response['html'] = $html;

      return response()->json($response);
}
1 like
Tchopa's avatar

@Nakov

You legend, that did it.

Thanks for your help, I really appreciate it. Do you mind letting me know what I had done wrong, so I can learn more about what to do for next time.

Was it a problem with the JS? That was my first JS script and I was using a guide and trying to apply it to my project.

Nakov's avatar

@Tchopa both were a problem. The JS was because you used $note->id when getting the URL, so that would either return just the same ID of the note or null. You can always use the Network tab and inspect if the request URL was containing the right data.

The second part was that in the URL you have {client} and {note} and you were not leveraging the Route Model Binding

which made the client id to always be used. And it just so happened that you had a Note with the same ID and that's why the response was always the same.

1 like
Tchopa's avatar

@Nakov Understood, thanks. I'll look into Route Model Binding.

Please or to participate in this conversation.