Problems with drop down menu actions
Moderator: General Moderators
Problems with drop down menu actions
I have a problem whereby i need to retrieve all names and ids from a database and store them in a drop down menu. This i can do fine, but I need to send an action to the browser to be read by the page, so i can set a session variable.
Basically, i want the action to send to the browser the link
"index.php?artistid='$artistid' where the variable is related to the artist selected.
The code i have is as follows;
$result = @mysql_query($query);
echo "<select name='Artist'><option>Select One</option>";
//Make the pull down menu to display all the artists in the database
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$artistid = $row['ArtistID'];
echo "<option value=\"index.php?artistid=", $artistid, " \">{$row['ArtistName']}</option>\n";
}
Sadly this doesnt work. I have tried several variations, all equally unsuccessful.
Please help
Basically, i want the action to send to the browser the link
"index.php?artistid='$artistid' where the variable is related to the artist selected.
The code i have is as follows;
$result = @mysql_query($query);
echo "<select name='Artist'><option>Select One</option>";
//Make the pull down menu to display all the artists in the database
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$artistid = $row['ArtistID'];
echo "<option value=\"index.php?artistid=", $artistid, " \">{$row['ArtistName']}</option>\n";
}
Sadly this doesnt work. I have tried several variations, all equally unsuccessful.
Please help
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
You may also want to [php_man]urlencode[/php_man] your url variables. (and also use [php_man]mysql_fetch_assoc[/php_man]($result) which is essentially equivalent to using mysql_fetch_array($result , MSQL_ASSOC) just w/o a numerical index)
eg:
eg:
Code: Select all
<?php
@$result = mysql_query($query);
echo "<select name="Artist"><option>Select One</option>"; //Make the pull down menu to display all the artists in the database
while($row = mysql_fetch_assoc($result)) // changed to mysql_fetch_assoc
{
echo "<option value="index.php?artistid=" . urlencode($row['ArtistID']) . "">{$row['ArtistName']}</option>\n";
}
echo "</select>"; // close up the list.
?>Still not working
Thanks for the help, but sadly it still isnt working.
I have made the changes mentioned, and used the urlencode but still no joy. When i select one of the names from the drop down, nothing is sent to the browser, it just shows that name in the menu.
Any ideas why it isn't submitting the value to the browser??
Cheers
I have made the changes mentioned, and used the urlencode but still no joy. When i select one of the names from the drop down, nothing is sent to the browser, it just shows that name in the menu.
Any ideas why it isn't submitting the value to the browser??
Cheers
Within a table, the function is called to make the drop down.
All the code i have for the table is included below. As you can see, is also have some hrefs which append to the browser fine. But the dropdown doesnt work.
All the code i have for the table is included below. As you can see, is also have some hrefs which append to the browser fine. But the dropdown doesnt work.
Code: Select all
<?php
<table width="12%" cellspacing="0" cellpadding="0" border="0" bgcolor="#CCCCCC" align="left">
<tr><td><strong>Choose an Information Category</strong></td></tr>
<tr><td><a href= "index.php?view=News">News </td></tr>
<tr><td><a href= "index.php?view=Interviews">Interviews</td></tr>
<tr><td><a href= "index.php?view=Biographies">Biographies </td></tr>
<tr><td><a href= "index.php?view=Reviews">Reviews </td></tr>
<tr><td>_____________________</td></tr>
<tr><td><strong> Select your favourite artist </strong></td></tr>
<tr><td><?php Pick_Fav_Artist(); ?></td></tr>
</table>
<?php
function Pick_Fav_Artist()
{
require_once ('../DatabaseConnect.php');//Connect to the database again to retrieve stories
$query = "SELECT ArtistID, ArtistName FROM artistbio ORDER BY ArtistName ASC";
$result = @mysql_query($query);
echo "<select name="Artist"><option>Select One</option>";
//Make the pull down menu to display all the artists in the database
while($row = mysql_fetch_assoc($result))
{
echo "<option value="index.php?artistid=" .urlencode($row['ArtistID']). " ">{$row['ArtistName']}</option>\n";
}
echo "<input type="submit" name="submit" value="Go" onSelect= "./index.php? ">";
echo "</select>";//Close the list
}//End of function Pick_Fav_Artist()
?>
?>- Bill H
- DevNet Resident
- Posts: 1136
- Joined: Sat Jun 01, 2002 10:16 am
- Location: San Diego CA
- Contact:
Well, a dropdown only functions as an element of a form.
You also can make the form submit automatically upon a selection in the dropdown, but that's a little more advanced.
Edit: nufferkay beat me to it, but if you aren't familiar with forms you probably don't want to tackle javascript.
The javascript he gave you needs to be within a form anyway.
Code: Select all
<?php
<form action="script_to_run.php" method="post">
// <select><optios>etc go here
<input type="submit">
</form>
?>Edit: nufferkay beat me to it, but if you aren't familiar with forms you probably don't want to tackle javascript.
The javascript he gave you needs to be within a form anyway.