简体   繁体   中英

pass an item id to from card to modal

so i have a 3 cards that has its value taken from database i want to get the id of the particular card that was clicked and send it to modal so i can echo it in the modal

i tried sending it to the button trigger as data-id=<?php $card_id?> but that doesn't seem to work

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>

You could look into using Ajax for this.

Keep data-id="<?php echo $card_id; ?>" in your button.

Create a php page to process the ID to be echoed:

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

Use a modal event to push the ID to your modal's body using a class declaration:

$(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);
               }
          });
     });
});

Put the class declaration in your modal body where you want the ID to show:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM