DannyM Posted January 29, 2008 Share Posted January 29, 2008 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"]; Quote Link to comment https://forums.phpfreaks.com/topic/88480-drop-down-list-not-retaining-values/ Share on other sites More sharing options...
Psycho Posted January 29, 2008 Share Posted January 29, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/88480-drop-down-list-not-retaining-values/#findComment-452928 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.