PHP and adding a list box

They have: 40 posts

Joined: Apr 1999

Hello,

I wanted to have a listbox that had options from a mySQL db.
For example,

I have 14 songs and I want to have a drop box list all the song titles.

I will have all the 14 songs listed and a drop box.
If you select from the drop box, it will jump down to that particular song.

Any help? I have tried some ideas but to no avail!

Thanks in advance,
Keith

They have: 850 posts

Joined: Jul 1999

Sorry I did not have time to test it, but the following should work

<form action="<?=$PHP_SELF?>">
<select name="song">
<?
//Define variables
$mysql_host = 'localhost';
$mysql_username = 'username';
$mysql_password = 'password';
$table = 'song';

//Connect to database and choose table
//connect to mysql
$cnx = @mysql_connect($mysql_host,$mysql_username,$mysql_password) or die ("Could not connect:".mysql_error());
mysql_select_db($mysql_database) or die ("Could not choose database:".$mysql_error());

//Select the SONG from the table
$query = mysql_query("SELECT Song FROM $table");
while($row = mysql_fetch_array($query))
{
     echo "<option value=\"".$row["Song"]."\">".$row["Song"]."</option>";
}
mysql_close($cnx);
?>
</select>
<input type="submit" name="submitsong" value="Submit Song">
</form>
'

I am not sure what you mean by "it will drop down to that particular song".

With the code above, you could use:

if($submitsong)
{
     //A song has been submitted, do something here
}
'

Hope that helps.

They have: 40 posts

Joined: Apr 1999

Thanks for taking the time to answer my question Rob!
That did it...

Thanks,
Keith

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.