-1

I need to make drop down list as a link to different pages. How do I do that using PHP, MySQL and HTML.

    <?php
mysql_connect('localhost','root','');
mysql_select_db('test');

$sql="select first_name from users";
$result=mysql_query($sql);

echo "<select First_name=''>";  
echo "<a href='index.html'>";
while($row=mysql_fetch_array($result)){

    echo ":<option value='".$row['first_name']."'>".$row['first_name']."</option>";

    }
    echo"</a>";
    echo"</select>";

?>
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

2 Answers2

0

You can't use links on the option tag, in order to do that, you need to use javascript. You can try to do something like this:

echo "<select name=\"First_name\" onchange=\"document.location='?'+this.value\">";

0

PHP is a server-side script and does not manipulate a page after a user has adjusted it. Like real time. Only javascript and others do that. PHP creates a page with what you want to see but if you need to change something bases on a dropdown use java. Here is a function that can do that. It unhides a div tag that can have your info you need.

<script type="text/javascript">
window.onload = function() {
    var eSelect = document.getElementById('dropdown');
    var divtag1 = document.getElementById('divtag1');
    var divtag2 = document.getElementById('divtag2');
    eSelect.onchange = function() {
        if(eSelect.selectedIndex === 1) {
            divtag1.style.display = 'block';
        }
        if(eSelect.selectedIndex === 2) {
            divtag2.style.display = 'block';
        }//or if you want it to open a url
        if(eSelect.selectedIndex === 3) {
            window.open("https://yourwebsite.com", "_NEW");
        }
    }
}
</script>
 echo "<div id=\"divtag1\" style=\"display:none;\">/*your code*/
</div>";
echo "<div id=\"divtag2\" style=\"display:none;\">/*your code*/
</div>";