-1
I’m trying to pass on the results of foreach for <span id="mostra_ref">
mouse click, but this way is passing only the last value of the function explode, even the script being inside the foreach. I need to show each clicked reference.
<div class="entry-summary">
<span id="mostra_ref"><?php echo @$referencia_1 ?></span>
//primeiro mostra um valor do banco de dados
//ao clicar em qualquer referência na classe sku imprime apenas F015
</div>
<div class="product-variations">
<ul class="variations list-unstyled">
<?php
$aux = explode(",", $referencia);
foreach($aux as $outras_refs) : ?>
<li>
<div class="thumbnails">
<a class="zoom">
<script type="text/javascript">
$('a').on('click',function(){
$('#mostra_ref').html('<?php echo $outras_refs; ?>');
});
</script>
<div class="sku"><?php echo$outras_refs; ?></div>
//IMPRIME F014,F013,F015
</a>
</div>
</li>
<?php
endforeach;
?>
</ul>
</div>
The problem is this: PHP is building the whole page. After PHP does its job, the page is sent to the browser. By then, everything that was PHP has already been replaced by the values you programmed. Look at the source code by the browser, it will be easier for you to understand what is being sent. You are sending 3 times the JS, each with a value, so what will be worth is the last. A solution would be you to put a different ID on each "a", and use $(<?php echo $id; ?>) on each one of them.
– Bacco
I was looking at it now, each value is right when I inspect each element. I’ll see how I can solve.
– denis
You could, for example, use one function only, and mount a JS variable in each one. Or even use a property
data-ref="<?php echo $ref; ?>"
at each anchor, and pick up the value at the onclick.– Bacco
When you say creating an id for each "a" is creating an increment type <a id="<? php echo $id++ ? >" and calling in Js $('<? php echo $id++ >'). on('click',Function(){ ..... ? did not work out. I took the script from foreach but still keeps getting the last result. I will try to approach with $("a"). attr(ː date-reference');
– denis
No need to complicate, just use $outras_refs itself to create the ID. And the script is out of the foreach, just once. Within Function is that you will get the correct ID and use in echo. But in JS this end part, not in PHP.
– Bacco
I did not succeed ! You can please illustrate just how to set the id in Javascript?
– denis
Just looking at your code, I realize you’re doing it wrong. There are better ways to do this, I don’t mean to judge, but this code needs to be improved and much, just seeing you put a Javascript function inside the loop is no longer a good thing to see. Also, putting the @ to disguise the mistake... well, I would do it all differently... so I won’t even bother to answer your question.
– Ivan Ferrer
If you’re not going to answer, you don’t need to comment. Everything you said about mistakes I know, but it’s not the problem at hand. This is a Q&A site, not a analytics site. @Ivanferrer
– denis
My comments are guidelines for your growth as a professional, you should learn from your mistakes, since you already recognize them, you should try to avoid them, and not "cover the sun with the sieve". Your question and your code need to be improved, so an answer here would not solve the question as a whole, as it would contribute to the extent of your problem. The site also has a comment area, and anyone working in the IT area is an analyst, so it makes no sense to post.
– Ivan Ferrer
Unfortunately the policy of handling errors of the company is @, for not using framework, and having more "time", my codes I handle. That’s all right, I understand what you’re saying, I appreciate the guidelines. @Ivan Ferrer
– denis