Page 1 of 1

Problems with drop down menu actions

Posted: Mon Feb 23, 2004 6:46 pm
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

Posted: Mon Feb 23, 2004 8:50 pm
by d3ad1ysp0rk
. instead of ,

Posted: Mon Feb 23, 2004 9:00 pm
by pickle
LiLpunkSkateR wrote:. instead of ,
I think he (or she) means to use "." rather than "," in your echo statement.

Posted: Mon Feb 23, 2004 9:02 pm
by d3ad1ysp0rk
ya (he), sorry for being less specific :P

Posted: Mon Feb 23, 2004 10:58 pm
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.
?>

Still not working

Posted: Tue Feb 24, 2004 6:59 am
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

Posted: Tue Feb 24, 2004 8:25 am
by Bill H
Perhaps a dumb question, but do you have this dropdown menu within a form?

Posted: Tue Feb 24, 2004 9:27 am
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()
?>
?>

Posted: Tue Feb 24, 2004 9:51 am
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.

Posted: Tue Feb 24, 2004 9:55 am
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.

Posted: Tue Feb 24, 2004 10:19 am
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??

Posted: Tue Feb 24, 2004 10:20 am
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.