+ 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>";
6 ответов
+ 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
+ 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 👍
+ 2
You forgot to define $i.
+ 2
Ipang what if user disabled JavaScript
+ 2
Ipang Yes you are right... I also tried the same.
+ 1
Maneren It came from my project so i forget to get the line before the code.