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

Andreas94's avatar

load content in modal from ajax

I'm trying to load votes in ajax from my controller, only it always returns me empty, and from the debugbar it seems to load the ajax page with the query, but it doesn't print the results in the modal

route

  Route::post('view_voto', 'AjaxController@view_voto');

controller

	public function view_voto(Request $request) {
	 if(!empty($request)){
	   $data = VotoGiochi::where('id_gioco', $request->input('id_gioco'))->get();;
	   //print_r($request->all());
	   return $data;
	 } else {
	    return 'asa';
	 }
	}

html

<div class="modal modal-primary fade" id="votoGiochi" tabindex="-1" role="dialog" aria-labelledby="primaryModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
      <h6 class="modal-title" id="primaryModalLabel">Voti per {{$giochi->nome}}</h6>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">×</span>
        </button>
      </div>
      <div class="modal-body" id="votehere">

      </div>
    </div>
  </div>
</div>

js

$(document).ready(function(){
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
   $('#votoGiochi').click(function(){
     var project_id = $(this).attr("id");
     $.ajax({
       url:"{{url('ajax/view_voto')}}",
       method:"POST",
       data: {
         "_token": "{{ csrf_token() }}",
         id_gioco : {{$info_giochi->id}}
       },
       success:function(data){
         $('#votehere').html(data);
         console.log(data);
         $('#votoGiochi').modal("show");
       }
     });
   });
});

Shouldn't print the string into array? Where is it that I am wrong?

0 likes
1 reply
Andreas94's avatar

I also tried with the get

js

$(document).ready(function(){
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });
   $('#votoGiochi').click(function(){
     $.ajax({
       url:"{{url('ajax/view_voto/'.$info_giochi->id)}}",
       method:"get",
       success:function(data){
         $('#votehere').html(data);
         console.log(data);
         $('#votoGiochi').modal("show");
       }
     });
   });
});

route

  Route::get('view_voto/{id}', 'AjaxController@view_voto');

controller

	public function view_voto(Request $request, $id) {
	 if(!empty($request)){
		 $data = VotoGiochi::where('id_gioco', $id)->get();
	   //print_r($request->all());
	   return $data;
	 } else {
	    return 'asa';
	 }
	}

if I visit the url with the browser it's work:

[{"id":33,"id_gioco":4633,"id_utente":2,"voto":"10","updated_at":"2019-01-03 16:59:56","created_at":"2019-01-03 16:59:56"},{"id":34,"id_gioco":4633,"id_utente":1,"voto":"10","updated_at":"2019-01-05 13:57:24","created_at":"2019-01-05 13:57:24"},{"id":35,"id_gioco":4633,"id_utente":8,"voto":"10","updated_at":"2019-01-12 16:02:22","created_at":"2019-01-12 16:02:22"},{"id":36,"id_gioco":4633,"id_utente":4,"voto":"10","updated_at":"2019-03-18 23:47:55","created_at":"2019-03-18 23:47:55"},{"id":37,"id_gioco":4633,"id_utente":9,"voto":"9","updated_at":"2019-03-19 12:50:36","created_at":"2019-03-19 12:50:36"}]

and the debugbar returns me the correct query

select * from `voto_giochi` where `id_gioco` = '4633'

but why does nothing appear in the modal yet?

Please or to participate in this conversation.