Solved

PHP Code

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

777 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