Ajax request with bootstrap modal in php

后端 未结 1 895
逝去的感伤
逝去的感伤 2021-01-26 03:05

I am trying to display some information in a bootstrap modal based on some values.

Here is the work flow

1.Display Hyperlinks in a loop with a title.

2.W

相关标签:
1条回答
  • 2021-01-26 03:58

    Assuming your foreach outputs links like this:

     <a href="#myModal" data-toggle="modal" data-id="123" class="btn-block">title</a>
     <a href="#myModal" data-toggle="modal" data-id="1234567" class="btn-block">another title</a>
    

    You could then invoke the click method instead of show.bs.modal in order to use $(this).

    So your code will change to:

     $('.btn-block').on('click',function(){
        var id = $(this).data('id');
        alert(id);
        $('.modal-body').html('loading');
    
           $.ajax({
            type: 'POST',
            url: '../Functions/form_process.php?action=getdetailsbyid',
            data:{id: id},
            success: function(data) {
              $('.modal-body').html(data);
            },
            error:function(err){
              alert("error"+JSON.stringify(err));
            }
        });
     });
    

    Here is an Example


    Otherwise you will need to use a unique id on all your outputted links (which you can do using a counter in your foreach)

    0 讨论(0)
提交回复
热议问题