2017-01-26 78 views
0

我有動態表,並在按鈕值我存儲每個ID的值。 我需要將該ID傳遞給模態並在模態中執行一些查詢。我會通過php來做到這一點。按鈕值傳遞到模態

<td><button="type" class="btn btn-success btn-sm" onclick="getNarudzbaId(this)" value="<?php echo $r['id'];?>" data-toggle="modal" id="modal" data-target="#modal_theme_success">Pregled <i class="fa fa-play position-right"></i></a></td> 

<script type="text/javascript"> 

    function getNarudzbaId(object) { 

     var x = $(object).attr("value"); 

      $.ajax({ 
      type: "GET", 
      url: "pregled.php", 
      data:{name:x}, 

      success: function(data){ 
       alert("return here if success") 

      } 
     }) 
     } 

    </script> 

其實我想要在模態窗口中傳遞值,它在同一個頁面上的按鈕是。

這裏是我的模態

<div id="modal_theme_success" class="modal fade"> 
<div class="modal-dialog"> 
    <div class="modal-content"> 
     <div class="modal-header bg-success"> 
      <button type="button" class="close" data-dismiss="modal">&times;</button> 
      <h6 class="modal-title">Stavke naloga</h6> 
     </div> 
     <div class="modal-body"> 
     <h6 class="text-semibold">Text in a modal</h6> 
     <p>Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem. Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p> 

     <hr> 
      <?php 
       $temp = $_POST['name']; 
       echo $temp; 
      ?> 
     <h6 class="text-semibold">Another paragraph</h6> 
     <p>Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros.</p> 
     <p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p> 
     </div> 
     <div class="modal-footer"> 
      <button type="button" class="btn btn-link" data-dismiss="modal">Close</button> 
      <button type="button" class="btn btn-success">Save changes</button> 
     </div> 
    </div> 
</div> 
</div> 

我從來沒有發送數據前,模態,任何幫助是非常讚賞

+0

嘗試讀取這個答案:http://stackoverflow.com/questions/10626885/passing-data-to-a-bootstrap-modal – DekiChan

回答

1

概念很簡單。首先,你需要得到的價值和通過jQuery打開模態窗口後發送請求按鈕的

<button id="btn" value="<?php echo $r['id'];?>"></button> 

獲得價值,發送請求

$('#btn').click(function() { 
    var id = $(this).val(); 

    $.ajax({ 
     type: 'get', 
     url: 'pregled.php' 
     data: {'name':id}, 
     success: function(data) { 
     // Show returned data on Modal 
     $('.modal-title').text(data.name); // ex: show result on title 
     } 
    }); 
}); 

點不發送值模態,這是關於顯示請求關於Modal的數據根據​​你的問題。

0

我已經用這種方法解決了它,如果發生類似問題,它可以幫助某人。

按鈕

td><button="type" class="btn btn-success btn-sm" onclick="getNarudzbaId(this)" value="<?php echo $r['id'];?>" data-toggle="modal" id="modal" data-target="#modal_theme_success">Pregled <i class="fa fa-play position-right"></i></a></td> 

腳本

<script type="text/javascript">   
    function getNarudzbaId(object) { 
     var x = $(object).attr("value"); 
     $.post('modal-body.php', {post:x}, 
     function(data) { 
      $('#print').html(data); // div where value is printed 
     }); 
    } 
</script>