Solved

PHP Code

Posted on 2011-03-18
2
193 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
2 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
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 500 total points
Comment Utility
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

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now