Home > Enterprise >  PHP make options clickable links in select tag
PHP make options clickable links in select tag

Time:06-24

I'm trying to make the options in the select tag clickable links, but I'm not able to click anything before the browser redirects to the first value it gets from the mysql array.

<select name="recipient" id="choice" onchange="location.href=this.value">
<?php 
foreach($all_users as $user):
 ?>
<option selected='' value="<?=$user['id']; ?>"><?= $user['firstname'] . ' ' . $user['lastname']; ?>
</option>                                   
<?php endforeach; ?>
</select>

screenshot

CodePudding user response:

I am not sure, what is your question exactly, but try my code hopefully you will get something

<select id="select-opt">
    <option value="https://www.google.com/">Google</option>
    <option value="https://www.facebook.com/">Facebook</option>
    <option value="https://www.youtube.com/">Youtube</option>
</select>


<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<script type="text/javascript">
     $(document).on('change', '#select-opt', function (e) {
        var value = $(this).val();
        window.location.href=value;
    });
</script>
  • Related