John Davidson

PHP make options clickable links in select tag

0 comments
Message:


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" class="form-select form-select-lg mb-3"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



My Answer:
Unfortunately, it is not possible to make the options in a select tag clickable links without using JavaScript. The onchange event that you are currently using will redirect the page as soon as an option is selected.

One way to achieve clickable links within the select tag is to use JavaScript to handle the click event on the options. Here is an example of how you can do this:

html





In this example, each option has a data-url attribute that contains the URL you want to redirect to when the option is clicked. The JavaScript code listens for the change event on the select tag and then redirects the page to the URL specified in the data-url attribute of the selected option.

Please note that this solution requires JavaScript to work, so make sure to include the script tag in your HTML file.

Rate this post

4 of 5 based on 9811 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us