I have a dropdown list (HTML select box) which gets values from this MySQL query:
"SELECT cdID, cdTitle FROM CD ORDER BY cdID"
The result is then stored in an associative array, which is then output to the dropdown list:
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdTitle'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
My issue is that I would like the user to see the title of the CD, but for the actual value to be "cdID" as that is the foreign key used in my database.
Just change the attribute echoed out for value
-
<?php
echo '<select name= "list" id="list">';
while ($row = mysqli_fetch_assoc($result)){
echo '<option value="'.$row['cdID'].'">'.$row['cdTitle'].'</option>';
}
echo '</select>';
?>
why not set the option value with the cdID. echo '<option value="{$row['cdID']}">{$row['cdTitle']}</option>';
or echo sprintf('<option value="%s">%s</option>',$row['cdID'],$row['cdTitle']);