Drop Down Menus

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
User avatar
partiallynothing
Forum Commoner
Posts: 61
Joined: Fri Nov 21, 2003 5:02 pm
Location: connecticut, usa

Drop Down Menus

Post by partiallynothing »

I would like to have static drop down menues. First off, I have no clue how to create on without javascript; is there any other way?! Also, I would like them to default to the last user selection (which is stored in a MySQL server). How can I accomplish this? I don't have any code to show you, as I have no clue how to do what I want. :)
ghost007
Forum Commoner
Posts: 49
Joined: Sat Nov 22, 2003 10:10 am

Post by ghost007 »

you only need javascript if you want to autosubmit the values. Otherwise you can create the drop down between <POST> tags and add a submit button.

hope this helps
siech
User avatar
dyconsulting
Forum Newbie
Posts: 14
Joined: Mon Dec 01, 2003 6:52 pm
Location: San Francisco

How to create dropdown menu defaulted to last user input

Post by dyconsulting »

If you like drop down to default to last user selection, then you are not talking about static drop downs. You need dynamic drop down. The way to do it is this:

1. Get value of last user selection from database.
2. While building a select list, use value from step 1 to identify which value should be selected.

Here is sample code:

Code: Select all

<?php
function get_default_value () {
	$db 	= mysql_connect ($host, $mysqluser, $mysqlpass); 
	mysql_select_db ($database,$db); 
	$query 	= "select value from user_value"; 
	$val 	= mysql_query ($query, $db); 
	$value 	= mysql_fetch_row ($val);
	if (!$value) { 
           return;
	} else
	   return $value[0];
} 

$dropdown = array ("red"=>"RED", "blue"=>"BLUE", "orange"=>"ORANGE");

$selected = get_default_value ();
 
foreach ($dropdown as $option=>$value) {
	$selected = ($option==$selected)? "selected" : "":    
	echo "<option = '$option' $selected>" . $value. "</option>";
}



?>
Post Reply