VOSH
VOSH

Reputation: 123

load options to an drop down list in html from when the page loads from database

I want to load options for a drop downlist when an html form loads.I already know how to do when I select first drop down and load for second.But when page loads how to do that.Your kind consideration given with this regard is highly appreciated.

Upvotes: 0

Views: 527

Answers (1)

MH2K9
MH2K9

Reputation: 12039

You can try something like below for loading deopdownlist option from database

<?php
 $con=mysqli_connect("localhost","user","password","db_name");
 if (mysqli_connect_errno()) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }
 $result = mysqli_query($con,"SELECT * FROM table_name");
 echo '<select id="select_1">';
 while($row = mysqli_fetch_array($result)) {
    echo '<option value="'.$row['column_name'].'">'.$row['column_name'].'</option>';
 }
 echo '</select>';
 mysqli_close($con);
?>

Upvotes: 1

Related Questions