简体   繁体   中英

HTML form drop down select issue

I've got a login form which I'm trying to simplify. It all worked when you manually inputted your username and password. Now I am wanting a drop down for the username box from the MySql database.

This is the code that I have put into the form and it drops down and shows all the users but when you select it, put the password in and click login it doesn't pass the username.

<?php
mysql_connect('localhost', 'user', 'password.'); 
mysql_select_db('database');

$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select username='sub1'>";
while ($row = mysql_fetch_array($result))   {
echo"<option value'" . $row['username'] ."'>" . $row['username'] ."</option>"; } echo "</select>";?>

Any Ideas Anyone

Thanks

Tom, always check your generated HTML to investigate errors.

Change your code to:

<?php
mysql_connect('localhost', 'user', 'password.'); 
mysql_select_db('database');

$sql = "SELECT username FROM users";
$result = mysql_query($sql);
echo "<select name='username'>"; // Note name attribute
while ($row = mysql_fetch_array($result))   {
  echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>"; // Note `=` sign after value
 } 
echo "</select>";
?>

replace your code with this:

<?php
mysql_connect('localhost', 'user', 'password.'); 
mysql_select_db('database');

$sql = "SELECT username FROM users";
 $result = mysql_query($sql);
echo "<select name='sub1'>";
while ($row = mysql_fetch_array($result)){
echo "<option value='" . $row['username'] ."'>" . $row['username'] ."</option>"; 
} 
echo "</select>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM