Retroisbest
Retroisbest

Reputation: 89

HTML Select box (populated by mySQL) on click display url from mySQL to textbox or div

I have a html select box that is currently populated from a mySQL table, each item in the row has a URL that i would like to be displayed in a DIV or texbox, i can populate the select box with each items value and URL (but of course this looks messy and the url is no clickable)

    <select id="item-list" style="margin: 0 !important; padding: 0 !important; width:100%" name="item[]" class="form-control name_list" required>
    <option value="" selected>Select a category</option>
    <?php $sql ="SELECT DISTINCT item,link FROM test_table ORDER BY item ASC";
    $result = mysqli_query($connect, $sql);
    while($sql = mysqli_fetch_array($result)) {
    echo "<option value='".$sql["item"]."'>". $sql["item"]." <href='".$sql['link']."'>".$sql['link']."</option>";
    } 
    ?>
    </select>   

The select box populates with the value and the url next to it but ideally i would like the URL to be displayed in a separate area, I'm thinking it has to do with the onchange event but not sure how to implement this.

Heres a screenshot of what ive currently generated enter image description here

Thanks

What im trying to achieve is whichever item i select in the select box the url part is displayed in a separate textbox or div.

Upvotes: 1

Views: 108

Answers (1)

C3roe
C3roe

Reputation: 96407

unfortunatley not as the value has to be something else and not the url link

(Comment refering to the solution suggested in using href links inside <option> tag)

Then put the URL into a custom data attribute instead, and read it from there in the onchange handler.

<option value="1234" data-url="http://...">Option text</option>

Upvotes: 1

Related Questions