Answer the question
In order to leave comments, you need to log in
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 } ?>
Answer the question
In order to leave comments, you need to log in
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 } ?>
PHP:
<?php foreach($products as $index => $product): ?>
echo '<div class="product-popup" data-popup="<?=$index?>"><?=$product->description; ?></div>';
<?php endforeach; ?>
<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>
<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 questionAsk a Question
731 491 924 answers to any question