php 使用mysql在php中动态选择列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19954366/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dynamic select list in php with mysql
提问by harsh
using select list with mysql created a list. I have a mysql data with c_code,Table center,and having data 1. id,2. name,3. code. i want to select name from mysql data after selecting name in data list want to show the code crosponding that name without using any submit button from select dropdown list, and the code shows in either label or in inputtext box.
使用带有 mysql 的选择列表创建了一个列表。我有一个带有 c_code,表中心的 mysql 数据,并且有数据 1。id,2。名称,3。代码。我想在数据列表中选择名称后从 mysql 数据中选择名称想要显示代码,而不使用选择下拉列表中的任何提交按钮,代码显示在标签或输入文本框中。
Here is my full code.
这是我的完整代码。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title
</head>
<body>
<form id="form1" name="form1" action="" , method="post" >
<div>
<label for="list">Center</label>
<select name="list">
<option value=''>-----SELECT-----</option>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$result = mysqli_query($conn, 'SELECT id,name,code FROM center');
while ($row = mysqli_fetch_assoc($result)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
?>
</select>
</div>
</form>
</body>
</html>
回答by Fyntasia
First, let's organize the code a bit...
首先,让我们稍微组织一下代码......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled 1</title>
</head>
<body>
<form id="form1" name="form1" action="" method="post">
<div>
<label for="list">Center</label>
<select name="list">
<option value=''>-----SELECT-----</option>
<?php
$conn=mysqli_connect('localhost','root','');
$result=mysqli_query($conn,'SELECT id,name,code FROM center');
while($row=mysqli_fetch_assoc($result)) {
echo "<option value='$row[id]'>$row[name]</option>";
}
?>
</select>
</div>
</form>
</body>
</html>
Second, dynamically doing anything in HTML is impossible with PHP, PHP is a server-side script and can not post back data after user manipulations without refreshing the page.
其次,PHP 不可能在 HTML 中动态地做任何事情,PHP 是一个服务器端脚本,在用户操作后不能在不刷新页面的情况下回发数据。
You are looking for an $.ajax(); solution. I'd suggest looking into it on http://api.jquery.com/jQuery.ajax/
你正在寻找一个 $.ajax(); 解决方案。我建议在http://api.jquery.com/jQuery.ajax/上查看它
Good luck!
祝你好运!
回答by MAQU
Hope its this what you need. you do a select on the id and name for the dropdown menu. after you selected an element the form ll be submitted and a new sql query ll return you the code. there are several other ways to return the code, this is just one easy way
希望这是你需要的。您对下拉菜单的 id 和 name 进行选择。选择元素后,将提交表单,新的 sql 查询将返回代码。还有其他几种返回代码的方法,这只是一种简单的方法
notice, i didnt use prepared statements! because i do not have the correct synatx in head right now... you should think about a general implementation of prepared statements.
注意,我没有使用准备好的语句!因为我现在脑子里没有正确的语法……你应该考虑准备语句的一般实现。
<form id="form1" name="form1" action="" method="post" >
<div>
<label for="list">Center</label>
<select name="list" onchange="this.form.submit()">
<option value=''>-----SELECT-----</option>
<?php
$conn = mysqli_connect('localhost', 'root', '');
$result = mysqli_query($conn, 'SELECT id,name FROM center');
while ($row = mysqli_fetch_assoc($result))
{
$selected = (isset($_POST['list']) && $_POST['list'] == $row['id']) ? 'selected' : '';
echo "<option value='$row[id]' $selected >$row[name]</option>";
}
?>
</select>
</div>
<div>
<?php
if (isset($_POST['list']))
{
$result = mysqli_query($conn, 'SELECT code FROM center WHERE id=' . $_POST['list']);
while ($row = mysqli_fetch_assoc($result))
{
echo $row['code'];
}
}
?>
</div>
</form>
回答by Ruo Chun Zeung
Try to echo $row
variables separately by concatenation.
So change this line
尝试$row
通过串联分别回显变量。所以改变这一行
echo "<option value='$row[id]'>$row[name]</option>"
to
到
echo "<option value=". $row[id] .">".$row[name]."</option>"
or you can use escape character like this
或者你可以像这样使用转义字符
echo "<option value={$row[id]}>{$row[name]}</option>"
I think now your select option would work.
我认为现在您的选择选项会起作用。
回答by Seth Czerepak
If I understand you right, you're trying to do the same thing I was before I discovered this solution...
如果我理解你是对的,那么在我发现这个解决方案之前,你正在尝试做同样的事情......
I was creating a dropdown for selecting how many results to show per page. Total of 6 options, which I stored in an array...
我正在创建一个下拉菜单,用于选择每页显示多少结果。总共 6 个选项,我将它们存储在一个数组中...
$page_sizes = array(10, 25, 50, 100, 200, 500);
Next, I used a "foreach" to build the options list...and an if statement to show the option as selected ONLY if it matched the current page size (page size was determined earlier in the script using the $page_size variable)...
接下来,我使用了“foreach”构建选项列表......才能显示选项,以显示选项,只有当它匹配当前页面大小(使用$ page_size变量在脚本中先前确定的页面大小)时才会显示选项。 ..
foreach($page_sizes as $pagesize_opt){
($pagesize_opt == $page_size) ? $pagesize_options .= '<option selected="selected" value="'.$pagesize_opt.'">'.$pagesize_opt.'</option>':
$pagesize_options .= '<option value="'.$pagesize_opt.'">'.$pagesize_opt.'</option>';
}
Then, I inserted my options into my drop down...
然后,我将我的选项插入到我的下拉列表中...
$page_size_select = '<strong>Results Per Page </strong><select id="analytics_page_size_select">'.$pagesize_options.'</select>';
You could do the same using a "while" iterator. Just check to see if the array element matches your selected element.
你可以使用“while”迭代器来做同样的事情。只需检查数组元素是否与您选择的元素匹配。
OR, if you want to get fancy and use an associative array which selects the option by the key instead of by the value, you can use the "key()" function to get the key and test whether it matches your selected key...
或者,如果您想花哨并使用通过键而不是值来选择选项的关联数组,您可以使用“key()”函数来获取键并测试它是否与您选择的键匹配。 .
http://php.net/manual/en/function.key.php
http://php.net/manual/en/function.key.php
So the entire code looks like this...
所以整个代码看起来像这样......
$page_sizes = array(10, 25, 50, 100, 200, 500);
foreach($page_sizes as $pagesize_opt){($pagesize_opt == $page_size) ? $pagesize_options .= '<option selected="selected" value="'.$pagesize_opt.'">'.$pagesize_opt.'</option>':
$pagesize_options .= '<option value="'.$pagesize_opt.'">'.$pagesize_opt.'</option>';
}
$page_size_select = 'Results Per Page '.$pagesize_options.'';
回答by Sawalhah
you should write select tag out of form because this list will not populated until form submitting
你应该写出表单的选择标签,因为这个列表在表单提交之前不会被填充