Jump to content

Drop Down List not retaining values?


DannyM

Recommended Posts

Hello, everybody.

 

I have run into a slight problem that I am hoping can be solved asap. I have my program to list all employees in a drop down box, then echo out that employees name when they hit the "View" button. However, the list does not retain the values of the name, so everytime I hit submit, I get no character. Here is a snippet of my code that is giving me the problem -

 

 


echo "<b><font size='3'>Select Employee</font></b>";
echo '<select>';
$sql=mysql_query("SELECT * FROM employees");
while($row=mysql_fetch_array($sql))
{
echo '<option value="name">' . $row['Name'] . '</option>';
}
echo '</select>';
echo '<input type="submit" name="view" value="View Hours"/>';
echo "</form>";
echo "</td</tr></table>";

echo $_POST["name"];

Link to comment
https://forums.phpfreaks.com/topic/88480-drop-down-list-not-retaining-values/
Share on other sites

Well, you don't show the opening form tag. but, let's assume you are using the POST option. There are several problems here

 

1. You need to give the SELECT element a name

1. You are setting the value of EVERY option to the string "name".

2. Your select list will be reset if you POST the page to itself.

 

<?php

$name = $_POST["name"];

echo "<b><font size='3'>Select Employee</font></b>";
echo '<select name="name">';

$sql=mysql_query("SELECT * FROM employees");
while($row=mysql_fetch_array($sql))
{
    $selected = ($row['Name']==$name)?' selected="selected"':'';

    echo '<option value="' . $row['Name'] . '"'.$selected.'>' . $row['Name'] . '</option>';
}
echo '</select>';

echo '<input type="submit" name="view" value="View Hours"/>';
echo "</form>";
echo "</td</tr></table>";

echo $name;

?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.