Problems with drop down menu actions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Devon
Forum Newbie
Posts: 4
Joined: Mon Feb 23, 2004 6:46 pm

Problems with drop down menu actions

Post by Devon »

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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

. instead of ,
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

LiLpunkSkateR wrote:. instead of ,
I think he (or she) means to use "." rather than "," in your echo statement.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

ya (he), sorry for being less specific :P
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

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:

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.
?>
Devon
Forum Newbie
Posts: 4
Joined: Mon Feb 23, 2004 6:46 pm

Still not working

Post by Devon »

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
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Perhaps a dumb question, but do you have this dropdown menu within a form?
Devon
Forum Newbie
Posts: 4
Joined: Mon Feb 23, 2004 6:46 pm

Post by Devon »

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.

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()
?>
?>
nufferkay
Forum Newbie
Posts: 24
Joined: Fri Nov 28, 2003 2:27 pm

Post by nufferkay »

It will only submit if you use JavaScript to make it submit. So to the select tag, you should add:

OnChange="this.form.submit()"

And you may want to add a "go" button just for those who have Javascript turned off.
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Well, a dropdown only functions as an element of a form.

Code: Select all

<?php
<form action="script_to_run.php" method="post">
// <select><optios>etc go here
<input type="submit">
</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.
Devon
Forum Newbie
Posts: 4
Joined: Mon Feb 23, 2004 6:46 pm

Post by Devon »

You also can make the form submit automatically upon a selection in the dropdown, but that's a little more advanced.
Thats exactly what i want to do. Can i do this without Javascript??
nufferkay
Forum Newbie
Posts: 24
Joined: Fri Nov 28, 2003 2:27 pm

Post by nufferkay »

Devon wrote:
You also can make the form submit automatically upon a selection in the dropdown, but that's a little more advanced.
Thats exactly what i want to do. Can i do this without Javascript??
Nope.

The only thing you can do without javascript is have it submit when a button is clicked.
Post Reply