B
B
Bogdan Bembenok2016-06-22 11:15:10
PHP
Bogdan Bembenok, 2016-06-22 11:15:10

How to pass the clicked product outside the loop?

Hello everyone, I have a problem, I have a loop:

<?php foreach ($products as $product) { ?>
    <?php } ?>

in which the products are displayed, outside the loop there is a template for a modal window, how can I transfer the clicked product to this window? something like $this

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Wolf, 2016-06-22
@mannaro

Clicks are JS. Rendering is PHP. Don't mix them up.
If you really need it, then something like this:

<?php foreach ($products as $product) { ?>
  <div onclick="showPopup('<?php echo json_encode($product) ?>')">Click Me!</div>;
<?php } ?>

Or store somewhere in the product code and pass it to popup by link.

S
Slava Vitrenko, 2016-06-22
@bagiroff777

PHP:

<?php foreach($products as $index => $product): ?>
  echo '<div class="product-popup" data-popup="<?=$index?>"><?=$product->description; ?></div>';
<?php endforeach; ?>

js:
<script>
  $(".product").on("click", function(e){
    var div = $(this);
    $("#modal-content").load(div.attr("/products/detail?id=" + div.attr("data-popup")));
    $("#modal-div").modal("show");
  }):
</script>

HTML:
<div id="modal-div" class="modal in" role="dialog">
  <div class="modal-dialog modal-lg">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
      </div>
      <div class="modal-body">
        <div id="modal-content"></div>
      </div>
    </div>
  </div>
</div>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question