<a> tag not working inside while loop on option tag in php | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

<a> tag not working inside while loop on option tag in php

echo "<select>"; while($i) { echo '<a href="pageToLink.php"><option>Something</option></a>'; $i--; } echo "</select>";

22nd Feb 2019, 6:51 PM
Praveen Soni
Praveen Soni - avatar
6 Answers
+ 6
I tried using links with options in <select> but it didn't work, even after having `$i` variable set in place. Maybe you can setup `onchange` event handler for the <select>, and use JavaScript to redirect instead, although I'm not sure if redirection works in Code Playground. <select id='links' onchange='surf_to()'> <option value=''>Choose a link</option> <?php $i = 5; while($i) { echo "<option value='pageToLink.php?linkId=$i'>Link $i</option>"; $i--; } ?> </select> <script> function surf_to() { var url = document.getElementById('links').value; if(url.length === 0) return false; console.log(url);//window.location.href = url; } </script> Hth, cmiiw
22nd Feb 2019, 8:54 PM
Ipang
+ 5
Praveen Soni Then redirection by the drop-down will not work, but I have tested, neither <a> inside <option> nor <option> inside <a> work with the drop-down. Maybe others can suggest a better option. Good luck 👍
23rd Feb 2019, 10:05 PM
Ipang
+ 2
You forgot to define $i.
22nd Feb 2019, 7:58 PM
Maneren
Maneren - avatar
+ 2
Ipang what if user disabled JavaScript
23rd Feb 2019, 5:19 PM
Praveen Soni
Praveen Soni - avatar
+ 2
Ipang Yes you are right... I also tried the same.
24th Feb 2019, 4:39 PM
Praveen Soni
Praveen Soni - avatar
+ 1
Maneren It came from my project so i forget to get the line before the code.
23rd Feb 2019, 5:20 PM
Praveen Soni
Praveen Soni - avatar