Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
PHP Array with Javascript and variable label id
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 470455" data-attributes="member: 1872"><p>You’re redeclaring changeLabel() over and over, so the ID inside is going to be the same for each record.</p><p></p><p>You have 2 options; either declare one function and allow and ID parameter to be passed in, or change the name of the function for each record.</p><p></p><p><strong>Option 1: pass an ID to the function (recommended)</strong></p><p>[code=php]<?php</p><p></p><p>$json = '[{"Id":"1","Icon":"1","FName":"Facebook","Sname":"ThisPasswordIsSecret"},{"Id":"2","Icon":"2","FName":"Instagram","Sname":"ThisPasswordIsSecret"},{"Id":"7","Icon":"3","FName":"Twitter","Sname":"ThisPasswordIsSecret"}]';</p><p>$array = json_decode($json, true);</p><p></p><p>?></p><p></p><p><?php foreach ($array as $record) { ?></p><p> <div class="flip"></p><p> <div class="card"></p><p> <div class="face front"></p><p> <img src="https://lh3.googleusercontent.com/XDScQo39jp9nk8kzAY_GOGYkb999mmMIqQGacISVnkGNc-tnZDZ1lD4valDhPOz7wxTm3pgk=s50-h50-e365" /></p><p> <div class="label2" id="<?php echo "label".$record['Id']; ?>"><?php echo $record['FName']; ?></div></p><p> <button class="viewbtn" onclick="changeLabel('<?php echo $record['Id']; ?>')">&#128275;</button></p><p> </div></p><p> </div></p><p> </div> </p><p> </p><p></p><p> <?php } ?></p><p><script></p><p> function changeLabel(id) {</p><p> let label = document.getElementById(`label${id}`);</p><p> label.innerText = "test";</p><p> setTimeout(function(){ document.getElementById(`label${id}`).innerHTML = 'Works';}, 3000);</p><p> </p><p> }</p><p> </p><p></script> [/code]</p><p></p><p><strong>Option 2: create a new function each time</strong></p><p>[code=php]</p><p><?php</p><p></p><p>$json = '[{"Id":"1","Icon":"1","FName":"Facebook","Sname":"ThisPasswordIsSecret"},{"Id":"2","Icon":"2","FName":"Instagram","Sname":"ThisPasswordIsSecret"},{"Id":"7","Icon":"3","FName":"Twitter","Sname":"ThisPasswordIsSecret"}]';</p><p>$array = json_decode($json, true);</p><p></p><p>?></p><p></p><p><?php foreach ($array as $record) { ?></p><p> <div class="flip"></p><p> <div class="card"></p><p> <div class="face front"></p><p> <img src="https://lh3.googleusercontent.com/XDScQo39jp9nk8kzAY_GOGYkb999mmMIqQGacISVnkGNc-tnZDZ1lD4valDhPOz7wxTm3pgk=s50-h50-e365" /></p><p> <div class="label2" id="<?php echo "label".$record['Id']; ?>"><?php echo $record['FName']; ?></div></p><p> <button class="viewbtn" onclick="changeLabel<?php echo $record['Id']; ?>()">&#128275;</button></p><p> </div></p><p> </div></p><p> </div> </p><p> </p><p><script></p><p> function changeLabel<?php echo $record['Id']; ?>() {</p><p> let label = document.getElementById('<?php echo 'label'.$record['Id']; ?>');</p><p> label.innerText = "test";</p><p> setTimeout(function(){ document.getElementById('<?php echo 'label'.$record['Id']; ?>').innerHTML = 'Works';}, 3000);</p><p> </p><p> }</p><p> </p><p></script></p><p></p><p> <?php } ?>[/code]</p><p></p><p>I typed this on my phone so it may be wrong.[/B]</p></blockquote><p></p>
[QUOTE="Markshall, post: 470455, member: 1872"] You’re redeclaring changeLabel() over and over, so the ID inside is going to be the same for each record. You have 2 options; either declare one function and allow and ID parameter to be passed in, or change the name of the function for each record. [B]Option 1: pass an ID to the function (recommended)[/B] [code=php]<?php $json = '[{"Id":"1","Icon":"1","FName":"Facebook","Sname":"ThisPasswordIsSecret"},{"Id":"2","Icon":"2","FName":"Instagram","Sname":"ThisPasswordIsSecret"},{"Id":"7","Icon":"3","FName":"Twitter","Sname":"ThisPasswordIsSecret"}]'; $array = json_decode($json, true); ?> <?php foreach ($array as $record) { ?> <div class="flip"> <div class="card"> <div class="face front"> <img src="https://lh3.googleusercontent.com/XDScQo39jp9nk8kzAY_GOGYkb999mmMIqQGacISVnkGNc-tnZDZ1lD4valDhPOz7wxTm3pgk=s50-h50-e365" /> <div class="label2" id="<?php echo "label".$record['Id']; ?>"><?php echo $record['FName']; ?></div> <button class="viewbtn" onclick="changeLabel('<?php echo $record['Id']; ?>')">🔓</button> </div> </div> </div> <?php } ?> <script> function changeLabel(id) { let label = document.getElementById(`label${id}`); label.innerText = "test"; setTimeout(function(){ document.getElementById(`label${id}`).innerHTML = 'Works';}, 3000); } </script> [/code] [B]Option 2: create a new function each time[/B] [code=php] <?php $json = '[{"Id":"1","Icon":"1","FName":"Facebook","Sname":"ThisPasswordIsSecret"},{"Id":"2","Icon":"2","FName":"Instagram","Sname":"ThisPasswordIsSecret"},{"Id":"7","Icon":"3","FName":"Twitter","Sname":"ThisPasswordIsSecret"}]'; $array = json_decode($json, true); ?> <?php foreach ($array as $record) { ?> <div class="flip"> <div class="card"> <div class="face front"> <img src="https://lh3.googleusercontent.com/XDScQo39jp9nk8kzAY_GOGYkb999mmMIqQGacISVnkGNc-tnZDZ1lD4valDhPOz7wxTm3pgk=s50-h50-e365" /> <div class="label2" id="<?php echo "label".$record['Id']; ?>"><?php echo $record['FName']; ?></div> <button class="viewbtn" onclick="changeLabel<?php echo $record['Id']; ?>()">🔓</button> </div> </div> </div> <script> function changeLabel<?php echo $record['Id']; ?>() { let label = document.getElementById('<?php echo 'label'.$record['Id']; ?>'); label.innerText = "test"; setTimeout(function(){ document.getElementById('<?php echo 'label'.$record['Id']; ?>').innerHTML = 'Works';}, 3000); } </script> <?php } ?>[/code] I typed this on my phone so it may be wrong.[/B] [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
PHP Array with Javascript and variable label id
Top