Jump to content

Recommended Posts

I am new to PHP and I'm having trouble getting my drop down box to work.  I just want a simple drop down box that displays a list of employees from a table.

 

Here is the code that I have been working with, I just don't know where my error is.  ???

 

$query = "SELECT DISTINCT `Name` FROM `employee` ORDER BY `Name`";
$result = mysql_query($query)
or die (mysql_error());

echo "<form action='viewEmp.php' method='POST'>
<select name='EmployeeName'>";


while($row = mysql_fetch_array($result)) // Loop thru recordset and get values
{
echo"<option value='".$row["Name"]."'>".$row["Name"];
}
echo"</select>";
echo"<input type='submit' Value='Submit'>
</form>";

?>

 

I appreciate any help anyone can provide.

 

Thanks

 

Joe

Link to comment
https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/
Share on other sites

1) Are you connected to a DB?  You code doesn't show that part if you are.

2) You shouldn't use single quotes in tag properties, like

<?php echo "<form action='viewEmp.php' method='POST'>

should be:

<?php echo "<form action=\"viewEmp.php\" method=\"POST\">

3) You are not closing your option tags with </option>

I am able to connect to the database and display my data when display it outside of my drop down menu.  I will post my entire code so everyone can view it with the changes mentioned by CroNiX.  When I changed my form tag to what CroNix suggested, I got this error message:

 

Parse error: syntax error, unexpected '/' in C:\wamp\www\hi\findEmp.php on line 41

 

Here is all my code:

 

<?php
echo "<html>
<head>
<title>View All Work Orders Completed by One Employee</title>
<link href='css/gobal.css' rel='stylesheet' type='text/css' />
</head>
<body>";

$host="localhost";
$user="root";
$password="";

$cxn = mysql_connect($host,$user,$password) or die ("Couldn't connect to database.");

$dbname = 'holidayinn';
mysql_select_db($dbname);

$query = "SELECT DISTINCT `Name` FROM `employee` ORDER BY `Name`";
$result = mysql_query($query)
or die (mysql_error());

echo "<div id='header1'><img src='images/hi_logo.gif' alt='Holiday Inn Logo' width='200' height='83' id='logo'/><p align='right'><a href='home.html' target='_self' class='headlink'>Home</a> </p><h1 align='center'>Maintenance Request Tracking</h1></div>
<div>";


while ($row = mysql_fetch_assoc($result)) {
    echo $row["Name"];
}


/*This is the form used to display the drop down box to select an employee. */
echo "<form action=\"viewEmp.php\" method=\"POST\">
<select name='EmployeeName'>";

while($row = mysql_fetch_array($result)) // Loop thru recordset and get values
{
echo"<option value='".$row["Name"]."'>".$row["Name"];
echo"</option>
}
echo"</select>";
echo"<input type='submit' Value='Submit'>
</form>";

?>
</div>

</body>
</html>

Wow, you are really mixing a lot of things that shouldn't be.

 

<html>
<head>
<title>View All Work Orders Completed by One Employee</title>
<link href="css/gobal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$host="localhost";
$user="root";
$password="";

$cxn = mysql_connect($host,$user,$password) or die ("Couldn't connect to database.");

$dbname = 'holidayinn';
mysql_select_db($dbname);

$query = "SELECT DISTINCT `Name` FROM `employee` ORDER BY `Name`";
$result = mysql_query($query)
or die (mysql_error());

echo "<div id=\"header1\"><img src=\"images/hi_logo.gif\" alt=\"Holiday Inn Logo\" width=\"200\" height=\"83\" id=\"logo\"/><p align=\"right\"><a href=\"home.html\" target=\"_self\" class=\"headlink\">Home</a> </p><h1 align=\"center\">Maintenance Request Tracking</h1></div><div>";


while ($row = mysql_fetch_assoc($result)) {
    echo $row["Name"];
}

/*This is the form used to display the drop down box to select an employee. */
echo "<form action=\"viewEmp.php\" method=\"POST\">";
echo "<select name=\"EmployeeName\">";

while($row = mysql_fetch_array($result)) // Loop thru recordset and get values
{
echo "<option value=\"".$row["Name"]."\">".$row["Name"];
echo "</option>";
}
echo"</select>";
echo"<input type=\"submit\" Value=\"Submit\"></form>";

?>
</div>
</body>
</html>

 

In my original post, for #2 I didn't mean just where I showed you, I meant properties in all html tags.

No prob, it gave me a nice little break from writing these stupid legal contracts for upcoming client work :)  It's the worst part about being an independent contractor.

 

Did it work?

 

I would also suggest rewriting your code so that you are mixing php and html as little as possible.

This:

1) makes your html much more readable

2) makes it a lot easier to maintain your PHP code

 

Something like this:

<?php include("functions.php"); //include your function(s) ?>
<html>
<head>
<title>View All Work Orders Completed by One Employee</title>
<link href="css/gobal.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header1">
<img src="images/hi_logo.gif" alt="Holiday Inn Logo" width="200" height="83" id="logo"/>
        <p align="right"><a href="home.html" target="_self" class="headlink">Home</a> </p>
        <h1 align="center">Maintenance Request Tracking</h1>
</div>

<form action="viewEmp.php" method="POST">
<select name="EmployeeName">
        <?php createOptions(); //execute your function defined in 'functions.php' ?>
        </select>
<input type="submit" value="Submit">
</form>

</body>
</html>

 

Then create a 'functions.php' page like:

 

<?php
function createOptions(){
$host="localhost";
$user="root";
$password="";

$cxn = mysql_connect($host,$user,$password) or die ("Couldn't connect to database.");

$dbname = 'holidayinn';
mysql_select_db($dbname);

$query = "SELECT DISTINCT `Name` FROM `employee` ORDER BY `Name`";
$result = mysql_query($query)
	or die (mysql_error());
while ($row = mysql_fetch_assoc($result)) {
	echo $row["Name"];
}

/*This is the form used to display the drop down box to select an employee. */
while($row = mysql_fetch_array($result)) // Loop thru recordset and get values
{
 echo "<option value=\"".$row["Name"]."\">".$row["Name"];
 echo "</option>";
}
} // End createOptions()
?>

 

This is a very basic example and there is really more that should be done, but I hope you see the benefit.

Thanks again for the advice on separating the html code from the php code.  I have learned a lot just from you.  The drop down box is still not being populated though. 

 

When I use this code, it will show me the list of employees so I know that the database is being accessed and the data is being read correctly.

 

while ($row = mysql_fetch_assoc($result)) {
    echo $row["Name"];
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.