簡體   English   中英

使用數據列填充下拉列表並獲取用戶的選定項目

[英]populating drop down list using data column and get user's selected item

我在這里使用數據庫中的數據填充下拉列表中的代碼。 它工作正常,但我卡在獲取用戶的選定項目並將其放在變量中,有人可以幫助嗎? 這是代碼:

<?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; 
?>

我假設您將下拉列表顯示為HTML表單? 如果是這樣,那么我將假設您使用的是“ select”元素,在這種情況下,您需要給該元素一個“ name”(HTML屬性),以便在提交表單時(大概使用HTTP POST) PHP代碼可以在POST數據中查找具有相同名稱的變量,其中包含所選值。 希望這不是太過分了!

我不確定我是否確切地了解您在尋找什么,但是從我得到的結果中,您需要為您的選擇項提供一個可以在HTTP POST中使用的名稱。 然后,如果您檢查POST是否已執行,則可以將一個變量設置為與此相等,然后稍后顯示它。

例如:

if(isset($_POST){
$variable = $_POST['name'];}

echo $ variable;

這可能是最簡單的形式,或者我可能想念您需要的內容,但希望這會有所幫助。

根據您的上述評論,您似乎做錯了,將代碼更改為:

<?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
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM