How can I send two values through a select?
I want to send the name and id field through the option tag. Right now as it is set up it is only sending id, how can send name as well to insert it in the data base?
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM c开发者_运维百科ategories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
Make the option value have this format: name$id
<select name="category_id" size="1"><br />';
$sql = "SELECT id, name
FROM categories
ORDER BY name";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs)) {
echo "<option value=\"".$row['name']."$".$row['id']."\">".$row['name']."</option>\n ";
}
echo'
</select>
then when you retrieve the data you can explode it as so
$option = explode("$", $_POST['category_id']);
echo $option[0]; // Name
echo $option[1]; // Id
If I understand you correctly, you want a hidden <input>
field and some javascript that writes the <option>
's text.
However, I can't think of a single application where that is desirable... The point of database normalization is to avoid duplication.
精彩评论