Page 1 of 1

populating a listbox

Posted: Wed Feb 10, 2010 1:54 pm
by mattman098
Hi all, I've just started learning PHP so apologies if what I'm about to post is full on ridiculous but I'm definately stuck...

basically I'm trying to populate a listbox from a database in php myadmin, and I can do it fine jus using PHP but I'm wanting to run it through HTML as well, i dont know if i need to use _POST or _GET to make this work, or if there's some other method but any help will be appreciated, here's my code...

PHP

Code: Select all

<?php
$con = mysql_connect("host", "database", "password"); 
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("dbname", $con);
 
$result = mysql_query("SELECT * FROM students");
 
 
echo "<table border='0'>";
 
 
echo "<tr>";
echo "<td rowspan=\"6\">";
echo "<select size=10 name=userList mutiple>";
while($row = mysql_fetch_array($result))
{
    echo "<option>$row[sname], $row[fname]</option>";
}
echo "</select>";
echo "</td>";
echo "</tr>";
echo "</table>";
 
mysql_close($con);
?>
HTML

Code: Select all

<html>
<body>
 
<form action="manageuser.php" method="post">
 
<table border="0">
 
<tr>
    <td rowspan="6">
        <select size=10 name=userList mutiple>
        <option value="fname">fname</option>
        <option value="fname">fname</option>
        <option value="fname">fname</option>
        </select>   
    </td>
    <td>
        User ID: 
    </td>
</tr>
</form>
</body>
</html>
note: the host dbname password etc are all changed, it not being 'localhost' is definately not my issue =P

thanks in advance

Re: populating a listbox

Posted: Wed Feb 10, 2010 2:15 pm
by klevis miho
Ok first of all in the php change:
echo "<option>$row[sname], $row[fname]</option>";
to this:
echo "<option>$row['sname'], $row['fname']</option>";

and in the html:
<select size=10 name=userList mutiple>
to this:
<select size=10 name="userList" mutiple>

Re: populating a listbox

Posted: Wed Feb 10, 2010 2:19 pm
by klevis miho
And another thing is that I would use ' over ", for example:

echo "<td rowspan=\"6\">";

to become

echo '<td rowspan="6">';