使用 select list 和 mysql 创建一个列表。我有一个mysql数据,带有c_code,表中心,并且有数据1.id,2.id。名字,3。代码。我想在数据列表中选择名称后从 mysql 数据中选择名称,希望显示与该名称对应的代码,而不使用选择下拉列表中的任何提交按钮,并且代码显示在标签或输入文本框中。
这是我的完整代码。
<!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>
最佳答案
首先,让我们稍微组织一下代码......
<!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>
其次,使用 PHP 不可能在 HTML 中动态执行任何操作,PHP 是服务器端脚本,无法在用户操作后不刷新页面的情况下回发数据。
您正在寻找 $.ajax();解决方案。我建议在 http://api.jquery.com/jQuery.ajax/ 上查看它
祝你好运!
关于php - php和mysql中的动态选择列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19954366/