?
Solved

PHP Code

Posted on 2011-03-18
2
Medium Priority
?
201 Views
Last Modified: 2012-05-11
The code below is part of a form that fulls data from a mysql database & displays that information in a browser. As you can see in the code, it pulls the employee name. My question is, is there any way I can edit this line of code that will allow me to add a drop down box that shows the other employees but keep the employee it pulls from the mysql database default?
<b>Employee: </b><br><input type="text" name="employee" size="15" maxlength="30" option value="'.$row[4].'" /> <br>

Open in new window

0
Comment
Question by:wantabe2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 35168264
Check this : http://www.phpro.org/tutorials/Dropdown-Select-With-PHP-and-MySQL.html

build your dropdown first and use :

$dropdown = dropdown(...,....,..

echo '<b>Employee: </b><br><input type="text" name="employee" size="15" maxlength="30" option value="' . $row[4] . '" />' . $dropdown . '<br>';

Open in new window

0
 
LVL 20

Accepted Solution

by:
Mark Brady earned 2000 total points
ID: 35168617
Well yes you can do that very easily. Let's assume a few things as I don't know what your database looks like but I'll make some stuff up. I will assume that you already know the name or ID of the chosen employee so we know who is selected. Try this on your page.

<?php
include("connection-code-or-script-here");
// Get the posted employee
$employee_selected = $_REQUEST['employee'];
########################################## Now we have the chosen employee if any


$employees_select = "<select name='employees' id='employees' />";
$employees_select .= "<option value=''>Please select an employee</option>";

$sql = "SELECT id,first,last FROM employees";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$id = "{$row['id']}";
$first = "{$row['first']}";
$last = "{$row['last']}";
if($id == $employee_selected){
$employee_select .= "<option selected value='$id'>$last $first</option>";
}else{
$employee_select .= "<option value='$id'>$last $first</option>";
}
}
$employee_select .= "</select>";
################################# END OF PROCESSING ##########################

now in your form, put all the controls you need to have and add this code.

Employee <?php echo $employee_select; ?><br />

Then continue with your form. If a user changes/selects an employee and submits the form, the selectbox will stay on the selected employee but all the other employees are still in the selectbox. If all you are doing is displaying employee data and only need a selectbox to choose the employee to display the data, then you could add            

onchange='this.form.submit()'

to the <select> tag so it submits the form when it is changed. Then you can do an if statement to see if an employee has been selected. If YES then show the employees data, if NO then just show the selectbox only . If you need help with that form just ask.

If this is not wehat you are looking for, please explain more about the form and perhaps post it here.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question