perrij3 Posted September 27, 2008 Share Posted September 27, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/ Share on other sites More sharing options...
CroNiX Posted September 27, 2008 Share Posted September 27, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652016 Share on other sites More sharing options...
perrij3 Posted September 27, 2008 Author Share Posted September 27, 2008 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> Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652021 Share on other sites More sharing options...
CroNiX Posted September 27, 2008 Share Posted September 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652024 Share on other sites More sharing options...
perrij3 Posted September 27, 2008 Author Share Posted September 27, 2008 Thanks for the help CroNix. As I said I am very new to PHP and really just trying to figure it out. I do appreciate the help you provided me. Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652028 Share on other sites More sharing options...
CroNiX Posted September 27, 2008 Share Posted September 27, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652035 Share on other sites More sharing options...
perrij3 Posted September 27, 2008 Author Share Posted September 27, 2008 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"]; } Quote Link to comment https://forums.phpfreaks.com/topic/126093-having-trouble-with-a-drop-down-menu/#findComment-652050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.