Solved

PHP Code

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

729 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