Jump to content

drop down menu - mysql


Recommended Posts

i want to have a drop down menu that interacts with my mysql db. all that i need help with is getting the options in a single column into the drop down menu as an <option> .

for example, my db is for track: name,event,time, school

for event, i want a drop down menu that will let me select whatever events are present in the event column of my db. such as the 55, 1600, 3200, etc. so how would i go about doing this?

Link to comment
Share on other sites


What server side tech are you using? No point in us giving out a full answer only to find out you're using something else...

And what is the actual problem you're having preventing you from doing this? Not sure how to generate the markup, or not sure what the SQL query should be? You need "event" to be unique or anything like that?

Also, why not have a EventID-like column (autoincrement, pri key) or such too?

Edit: forgot to add, does it need to do anything else like only list events that haven't "expired" or such?

Usually, what I'd want is something like this

<select name="events">

<option value ="1">Event Name</option>

<option value ="2">Whatever</option>

...

<option value ="n">Last Entry</option>

</select>

Where the number is the EventID (or just "id" or whatever) associated with the name, and you have the event name displayed.

Edited by crahak
Link to comment
Share on other sites

i'll be using php. and i did get some scripts to work

<?php
$track = mysql_query( "SELECT event FROM track" )
or die( "Cannot retrieve db/table" );
echo " <select name='event' onChange='submit(this.form)'>\n";
while( $row = mysql_fetch_row($track)) {
$sel = ( $table === $row[0] ) ? "id='sel' selected" : "";
printf( " <option %s value='%s'>%s</option>\n", $sel, $row[0], $row[0] );
}
echo " </select>\n";
echo " <input id='edit' type='button' value='Go' onClick='submit(this.form)'>\n";
?>

but now i have no clue how to get it to actually return only the values i selected.

Link to comment
Share on other sites

okay i have everything pretty much working now, but heres one that im really stuck on. i posted this somewhere else

----

i know how to make a drop down menu, but after i select something and click go, it uses php and loads up some data from a mysql db. it just refreshed the same page. but what i want it to do is to is make whatever i last picked in the drop down menu be what the SELECTED option is when the page is refreshed.

ex:

options are bob,joe,jim

since bob is 1st it automatically defaults to bob. but say i select jim and click go, i want jim to be what the defualt choice is.

Link to comment
Share on other sites

You've got exactly what I did a little while ago...

  $subjects = safe_query($subject_sql);
echo "<form action=\"index.php\" method=\"get\" >
Select Department:
<select onchange=\"java script:this.form.submit();\" name=\"Subject\">
<option value=\"0\">All</option>";
while($row= mysql_fetch_array($subjects))
{
echo "<option value=\"$row[ID]\"";
if( $row[ID] == $subjectID){ echo " SELECTED"; }
echo ">$row[subject]</option>";
}
echo "</select>
<noscript><input type=\"submit\" name=\"SelectExams\" /></noscript>
</form>";

Notice the "SELECTED" there? That'll make the drop down list have that specific element chosen by default. :)

Link to comment
Share on other sites

how exactly am i suppose to use the code?

i have the drop down menu working with my db, i can get the info i need, but how do i make the name i selected stay there? it doesnt..

edit: this is odd. it wont work in my main script, but it does work in a quick thing that i made that has some of my script in it.. ill have to try to rebuild it and see what i messed up..

and i had a problem with one if your lines

if( $row[lastname] == $_POST['lastname']){ echo " SELECTED"; }

i had to do that instead, and it works, otherwise the name wont stay when i select it

Edited by ripken204
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...