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
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;

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.