![](/img/trans.png)
[英]how to get id of each selected item from two different drop down list using ajax
[英]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.