Click to See Complete Forum and Search --> : Text field Drop down menu


The Little Guy
09-24-2006, 10:33 PM
I have no clue where to post this, so... since there will be some PHP involved, I'll try here.

I want to have a text field that is also a drop down menu that is populated from a database (populated using PHP).

The user can either do 1 of 2 things:
type in the text, or use the drop down.

1. It would by typed in if its not in the database, and/or they feel like just typing it in.

2. It would be selected from the drop down, if it is in the database, and/or they don't want to type it.

Taschen
09-25-2006, 03:31 AM
Not only PHP but also a bit of Javascript. Basically there are two parts to this, first populate a drop down menu from a database (that's the PHP part) then using javascript and an onChange (or similar command) either populate the text input field or using the text input field add and option to the drop down menu (xhtml doesn't allow you to type into a select menu hence the need for a text input field.)


$query = ("SELECT some_id, some_name FROM some_table WHERE some AND condition ORDER BY some_id ASC;");
$result = mysql_query($query) or die ('Error in query: $query.'.mysql_error());

if (mysql_num_rows($result) > 0){ // Providing there are some results to show go ahead and show them!
//Read you results into a variable
while ($row = mysql_fetch_object($result)){
$id = $row -> some_id;
$MENU[$id]['name'] = $row -> some_name;
}
mysql_free_result($result); // Flush the result
}

//Begin your etc, and then somewhere in your html
//
echo "<select id=\"menu\" name=\"os1\" onchange=\"java_function(this);\" class=\"a_class\">\n";
echo "<option value=\"0\" selected >Select something</option>\n";
foreach ($MENU as $_KEY => $_VAL){ //Begin drop down menu
echo "<option value=\"".$_KEY."\">".$_KEY."</option>\n";
}
echo "</select>\n"; //End (the \n is a new line symbol).
//
//And your text input box
echo '<input name="name_menu" id="name_menu" type="text" value="" class="a_class" />';


Next you will need some java (sorry about how crude this is):

<!-- Begin location for category quick jump menu
function java_function(this){
var this= document.form.os1.value;
document.write.name_menu.this;
}
// End -->


Hope that helps.