[英]How make drop-down in php have the option DB value as selected in html template
我在 html 中有一個下拉列表,它是從 php 模板呈現的。 我可以編輯從數據庫中獲取用戶名的下拉選項,但是如何在編輯之前“選擇”它(我之前選擇的)?
模板代碼如下。
$result = $conn->query("select username from users");
echo "<html>";
echo "<body>";
echo "<select name='workers' >";
while ($row = $result->fetch_assoc()) {
unset($username);
$username = $row['username'];
echo '<option value=" '.$username.'" >'.$username.'</option>';}
echo "</select>";
echo "</body>";
echo "</html>";
?>
請幫忙!
您需要給 name 以選擇標簽並獲得該名稱,例如:
<?php
$conn = new mysqli($servername, $username, $password, $dbname) or die ('Cannot connect to db');
$result = $conn->query("select username from users");
echo "<html>";
echo "<body>";
echo "<select name='drpdown'>";
while ($row = $result->fetch_assoc()) {
unset($username);
$username = $row['username'];
echo '<option value=" '.$username.'" >'.$username.'</option>';
}
echo "</select>";
echo "</body>";
echo "</html>";
?>
獲取 post 變量,如:
print_r($_POST['drpdown']);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.