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
Solved

PHP Code

Posted on 2011-03-18
2
197 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
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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

809 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