Pendig bootstrap modal in click to download deleted data

I have this code to load dynamic data from a remote file into bootstrap using jquery ajax:

JS:

$(function(){

   $('.push').click(function(){
      var essay_id = $(this).attr('id');
        $.ajax({
        type : 'post',
        url : 'your_url.php', // in here you should put your query 
        data :  'post_id='+ essay_id, // here you pass your id via ajax .
                     // in php you should use $_POST['post_id'] to get this value 
       success : function(r)
           {
              // now you can show output in your modal 
              $('#mymodal').show();  // put your modal id 
             $('.something').show().html(r);
           }
    });
 });
});

HTML:

<a href="#" id="1" class="push">click</a> 

<div class="modal-body">  
   <div class="something" style="display:none;">
     // here you can show your output dynamically 
   </div>
</div>

this worked for me, but the modal frame does not show until loading / pending data loading. I need to load a modal block after clicking, and then load the data.

How to fix it?!

+4
source share
1 answer

You can make an ajax call outside of your click event at startup and show hide on click:

$(document).ready(function() {

var essay_id = $(this).attr('id'); 
var results;

$.ajax({ 
      type : 'post', 
      url :  'your_url.php',
      async: false
      'post_id='+ essay_id, 
      success : function(r) { 
          results = r;
      } 
 });

$(' your_element').click(function() {
      if (results) {
          $('#mymodal').show(); 
          $('.something').show().html(results); 
      } 
 });
 });

Using async: false will terminate your request before continuing with the script. Hope this helps.

0

Source: https://habr.com/ru/post/1619941/


All Articles