...

View Full Version : Drop down Menu



gismo
05-03-2004, 04:40 PM
Hi all,

Would anyone have any ideas about how i would create a drop down menu, whose options are taken directly from a table on a mysql database?

Thanks,
Gismo

SDP2006
05-03-2004, 06:07 PM
<?php
$link = mysql_connect("localhost","username","password") or die(mysql_error()); /* connect to db */
mysql_select_db("db") or die(mysql_error()); /* select db to work from*/
$query = "SELECT id,option FROM menu_table ORDER by ID ASC"; /* set query */
$result = mysql_query($query) or die(mysql_error()); /* run query */
if($result){ /* if sucessful */
if(mysql_num_rows($result) == "0"){ /* check for no information */
echo "No information in database table!";
}
else{ /* if information is present in table, proceed to make the form */
echo '<form action="whatever.php" name="menu" method="POST">';
echo '<select size="1" name="menu">'; /* echo outside of the while() because we don't want a lot of different drop down boxes! */
while($row = mysql_fetch_assoc($result)){
echo '<option>'.$row['option'].'</option>'; /* each individual result will make an option */
}
echo '</select>'; /* end the select box */
echo '</form>';
}
}
?>Should work! Hopefully, i got all of my '}'s in there - I typed it fast. lol.

Hope this helps!

gismo
05-04-2004, 01:02 PM
hi,

Thank you for the reply, the code brings up a drop down menu, but all of the options are showing up blank in the drop down menu. It is clear that it is getting all the values from the database but is showing them blank. Any suggestions?

Thanks,
Gismo

SDP2006
05-04-2004, 01:14 PM
Can you give a link to the page? It would help If I could see the HTML source of the page to see whats going on.

gismo
05-04-2004, 01:30 PM
Hi i cant give a link as i dont have it up on the web, this is a project for college! But here is the code, hopefully this will help:
<html>
<title></title>
<head></head>
<body>
<td BGCOLOR="#66ccff"><br><center>
<FONT SIZE = 3 face = "ARIAL" COLOR = "#FFFFFF"><b>Step 2: Add student details</b><br><br>
By Class&nbsp;&nbsp;


<?php
$link = mysql_connect("localhost","root") or die(mysql_error()); /* connect to db */
mysql_select_db("teachers") or die(mysql_error()); /* select db to work from*/
$query = "SELECT class_name FROM classes"; /* set query */
$result = mysql_query($query) or die(mysql_error()); /* run query */

if($result){ /* if sucessful */
if(mysql_num_rows($result) == "0"){ /* check for no information */
echo "No information in database table!";
}

else{ /* if information is present in table, proceed to make the form */

echo '<select size="1" name="menu">'; /* echo outside of the while() because we don't want a lot of different drop down boxes! */

while($row = mysql_fetch_assoc($result)){
echo '<option>'.$row['option'].'</option>'; /* each individual result will make an option */
}
echo '</select>'; /* end the select box */
echo '</form>';
}
}
?>


<form method="post" action="register.php">
<input type="submit" name="button_submit" value = "SELECT">

</td>
</form>
</body>
</html>

Thanks,
Gismo

gismo
05-04-2004, 05:43 PM
i got it working, thanks for all your help!

SDP2006
05-05-2004, 03:57 AM
Glad to help :thumbsup:

fyogi1
03-14-2005, 05:13 PM
Is there any way to have the items in the drop down menu then post to a new php when you select an option? Instead of having a submit button.

I want the drop down menu to query the table and get the first column like it does in the above script. Then I want the user to select one and use that option in my next query on a new php but without the submit button.

Thanks
fyogi1

Kurashu
03-14-2005, 10:36 PM
<form method="post" name="form">
<select onchange="form.submit()">
[..options..]
</select>
</form>

fyogi1
03-15-2005, 07:35 PM
I think you forgot to and the action attribute. I got it to work like this:

<form method="POST" action="test.php">
<select name="name" onchange="form.submit()">
[..options w/ value attribute....]
</select>
</form>

Frank

drdre
03-15-2005, 10:53 PM
i have another question:

what if i want to to call a certain file based on what i selected, i.e. i select from the drop down the word "apple" and it opens up a page called "apple_site.html" or more general: i choose word "word" and it opens up the file associated with the word "word". is that possible with a sql database and php?

Ultragames
03-15-2005, 11:05 PM
Two ways. One: PHP. You make all of the options go to a page, get the value of the select menu, and redirect to the correct page through header('Location: WhatEver.html')

Two: Javascript, make an onChange function that gets the selected value of the drop down menu, and changes the form action acordingly.

The PHP way is far better. Universal, and easy.

drdre
03-16-2005, 12:13 AM
thanks.

but how do i get the selected value with php exactely? :confused:

mcdougals4all
03-16-2005, 12:50 AM
Get the value of the selected option from the POST array and redirect to the appropriate page.

The form:

<form id="nav" method="post" action="nav.php">
<div>
<label for="nav_select">Go to:</label>
<select id="nav_select" name="nav_select">
<option value="http://yoursite.com/page1.php">Page 1</option>
<option value="http://yoursite.com/page2.php">Page 2</option>
<option value="http://yoursite.com/page3.php">Page 3</option>
</select>
<input type="submit" value="Go" id="go" />
</div>
</form>

The form handler [referenced as nav.php above]:

<?
$redirect_to = $_POST['nav_select'] ;

header( "Location: $redirect_to" );
exit ;
?>

drdre
03-17-2005, 03:02 PM
thanks a lot, that's great!

now i have another question :)

in your example: if i want that page1.php displays a certain image based on what i selected before in the drop down *how do i do that?

background: in my project: i genereate a drop down from a database and then when i select its value, i.e. california it should go directly to a page where i see an image named california.jpg

so with the solution you told me: the page1.php is the page california.php. but since i have 200 of such pages i don't want to edit 200 html files to change the image all the time. so it should generate my new california page only based on the value from the drop down.

maybe it's a bit unclear but just ask me if you don't know how i mean it



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum