将商品ID从卡片传递到模式

时间:2019-10-16 18:18:18

标签: javascript php jquery mysql bootstrap-modal

所以我有3张卡,其值取自数据库 我想获取被单击的特定卡的ID并将其发送给模态,以便我可以在模态中回显

我尝试将它作为data-id = <?php $ card_id?>发送到按钮触发器,但这似乎不起作用

php & mysql codes to fetch from database  

<div class="container">
<div class="title">
    <h5><?php echo $card_id; ?></h5> <h1><?php echo $card_title; ?></h1>
    <div class="body">
        <?php echo $card_name; ?>
        <button type="button" data-toggle="modal" data-target="#myModal" name="button" data></button>
    </div>
</div>

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    <h1><?php echo $card_id; ?></h1>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以考虑使用Ajax。

在按钮中保留 data-id="<?php echo $card_id; ?>"

创建一个php页面来处理要回显的ID:

if ($_POST['cardid']) {
     $id = $_POST['cardid'];
     echo $id;
}

使用模态事件通过类声明将ID推送到模态主体:

$(document).ready(function(){
     $('#myModal').on('show.bs.modal', function(e){
          var cardid = $(e.relatedTarget).data('id');
          $.ajax({
               type: 'post',
               url: 'record.php',
               data: 'cardid='+ cardid,
               success: function(data){
                    $('.fetched-data').html(data);
               }
          });
     });
});

将类声明放在您要显示ID的模式主体中:

<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Modal Header</h4>
      </div>
      <div class="modal-body">
        <h1 class="fetched-data"></h1>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
      </div>
  </div>
</div>