I am working on another project that calls for a dropdown box to be populated with Salesman names. I have setup the database to pass the id
around, as opposed to the salesman's name. I want to know if there is a way to include a "hidden" column within a dropdown box, and call the id
as opposed to the name
.
There could possibly be salesman with the same first and last name, so I can't use a UNIQUE column to help (by using jQuery to re-query the database based on name
.
I'm open to suggestions if there might be a more appropriate way to do this. Thanks in advance!
function getSalesmen() {
include 'scripts/mysql_login_pdo.php';
$query = "SELECT `id`, `fname`, `lname` " .
"FROM `users` " .
"ORDER BY `fname` ASC";
$statement = $db->prepare($query);
$statement->execute();
$salesman = '';
while ($row = $statement->fetchObject()) {
$fname = $row->fname;
$lname = $row->lname;
$salesman .= '<option>';
$salesman .= $fname . ' ' . $lname; //This is where I'd like the ID to somehow be handled
$salesman .= '</option>';
}
$db = null;
return $salesman;
}
Relevant section of index.php-
<li>
<form method="post">
<ul>
<li>Salesman</li>
<li><select id="salesman" name="salesman"><?php print getSalesmen(); ?> </select></li>
<li>Status</li>
<li><select><?php print getStatus(); ?></select></li>
<li><button type="button" id="search_a" name="search_a">Search</button><br />
<button type="button" id="fill_all_a" name="fill_all_a">All Assigned Leads</button></li>
</ul>
</form>
</li>
Just use the value
attribute of your option
tag
$salesman .= '<option value="'.$row->id.'">';
$salesman .= $fname . ' ' . $lname;
$salesman .= '</option>';
Now the id
of the selected salesman will be posted.
Use the "value" attribute (http://www.w3.org/wiki/HTML/Elements/select#Examples)