php - 使用数据列填充下拉列表并获取用户选择的项目

标签 php mysql variables drop-down-menu

我这里有使用数据库中的数据填充下拉列表的代码。它工作正常,但我坚持获取用户选择的项目并将其放入变量中,有人可以帮忙吗?这是代码:

<?php   
// declare database connection variables.
$host = "localhost";
$username = "root";
$password = "";
$db_name = "sample";
$tbl_name = "tbl_company";

// connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT type FROM $tbl_name";
$result = mysql_query($sql) or die(mysql_error());

$dropdown = "<select name='items' class='select'>";

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}
$dropdown .= "\r\n</select>";

echo $dropdown; 
?>

最佳答案

根据你上面的评论,你似乎做错了,将你的代码更改为:

<?php   
// declare database connection variables.
$host = "localhost";
$username = "root";
$password = "";
$db_name = "sample";
$tbl_name = "tbl_company";

// connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql = "SELECT type FROM $tbl_name";
$result = mysql_query($sql) or die(mysql_error());

$dropdown = "<form action='' method='post'>"; //form tag added here,  
                                             //this is absolutely needed

$dropdown .= "<select name='items' class='select'>";

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<option value='{$row['type']}'>{$row['type']}</option>";
}
$dropdown .= "\r\n</select>";
$dropdown .= "<input type='submit' name='submit' value='Submit'>";
$dropdown .= "</form>"; //closing the form tag
echo $dropdown; 

//checking if user has submitted the form
if(isset($_POST['submit']))
{
  $var = $_POST['items'];
  echo $var;
}
?>

编辑:

根据 OP 的以下评论:

$dropdown = "<form action='' method='post'>"; //form tag added here,  
                                             //this is absolutely needed

while ($row = mysql_fetch_assoc($result)) {
    $dropdown .= "\r\n<input type='checkbox' name='items[]' value='{$row['type']}'>{$row['type']}";
}
$dropdown .= "<input type='submit' name='submit' value='Submit'>";
$dropdown .= "</form>"; //closing the form tag
echo $dropdown; 

//checking if user has submitted the form
if(isset($_POST['submit']))
{
  //$_POST['items'] is now a multi dimensional array
  //because we named our checkboxes as items[]
  //if you don't understand how, learn about input array
  //it's a little long to be explained here
  //therefore, you need the foreach statement to retrieve its values
  foreach($_POST['items'] as $var){
   echo $var; //prints all the checked values
  }
}

关于php - 使用数据列填充下拉列表并获取用户选择的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22068398/

相关文章:

javascript - 将 PHP 页面输出插入 jquery 变量

javascript - 在Nodejs中重新分配变量时,存储旧变量的内存会释放吗?

javascript - JavaScript 闭包是如何工作的?

php - 隐藏 URL 的 index.php(或 index.html)

javascript - AES-256-CBC 解密 - Cryptojs 加密和 PHP 解密

javascript - 显示输入类型文本的数量等于我的输入文件中选定文件的数量,无需发送表单

mysql - 在 MySQL 中存储/更新基于比较的数据

php - cURL 请求花费的时间太长,代码有问题吗?

mysql - 使用 ruby​​ 将变量写入 mysql 的事件记录

mysql - 我正在尝试连接两个表,但可以获得预期结果