尝试从mysql数据库动态更改下拉列表值。脚本似乎在工作,但我不确定。该数据库列ID,columnA,columnB,columnC,columnD我们如何使用数据库动态更改一些下拉列表?
在第二个下拉列表选择,它提供了警报“喜试”。
$(document).ready(
function() {
$(".columnA").change(
function() {
var dataA=$(this).val();
var dataString = 'columnA='+ dataA;
$.ajax ( {
type: "POST",
url: "ajax_try.php",
data: dataString,
cache: false,
success: function(html)
{
$(".columnB").html(html);
}
}
);
}
);
$(".columnB").change(
function() {
var dataB=$(this).val();
var dataString = 'columnB='+ dataB;
$.ajax ( {
type: "POST",
url: "ajax_try.php",
data: dataString,
cache: false,
success: function(html)
{
alert("hi.. again");
$(".columnC").html(html);
}
}
);
}
);
});
<?php
include('db.php');
if($_POST['columnA'])
{
$columnA = $_POST['columnA'];
$sql = mysql_query("SELECT id, columnB FROM try WHERE columnA = '$columnA' GROUP BY columnB");
//$sql = mysql_query("SELECT id, columnB FROM try columnB WHERE columnA = '$columnA'");
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
$columnB = $row['columnB'];
echo '<option value="'.$id.'">'.$columnB.'</option>';
}
}
if($_POST['columnB'])
{
$columnB = $_POST['columnB'];
$sql = mysql_query("SELECT id, columnC FROM try WHERE columnB = '$columnB' GROUP BY columnC");
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
$columnC = $row['columnC'];
echo '<option value="'.$id.'">'.$columnC.'</option>';
}
}
?>
好吧,我想HTML代码,使其更易于理解的问题..
<html xmlns="http://www.w3.org/1999/xhtml">
....
<script type="text/javascript" src="http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
.....
here is the script
...
</script>
</head>
<body>
<table width="100%" border="0" cellpadding="10">
<tr>
<td width="15%"><span style="font-size:20px;">columnA</span></td>
<td><span >
<select size="1" id="columnA" title="" name="columnA" class="columnA" style="width:250px; height:40px; font-size:20px; padding: 5px;">
<option value="0" selected="selected">Choose..</option>
<?php
include('db.php');
$sql=mysql_query("SELECT columnA FROM try GROUP BY columnA");
while($row = mysql_fetch_array($sql))
{
//$id = $row['id'];
$dataA = $row['columnA'];
echo '<option value="'.$dataA.'">'.$dataA.'</option>';
}
?>
</select>
</span>
</td>
</tr>
<tr>
<td width="15%"><span style="font-size:20px;">columnB</span></td>
<td>
<select size="1" id="columnB" title="" name="columnB" class="columnB" style="width:250px; height:40px; font-size:20px; padding: 5px;">
<option value="0" selected="selected">Choose..</option>
</select>
</td>
</tr>
<tr>
<td width="15%"><span style="font-size:20px;">columnC</span></td>
<td>
<select size="1" id="columnC" title="" name="columnC" class="columnC" style="width:250px; height:40px; font-size:20px; padding: 5px;">
<option value="0" selected="selected">Choose..</option>
</select>
</td>
</tr>
</table>
</body>
</html>
你能更清楚地解释您遇到什么问题? – 2012-04-18 12:02:00
你可以发布你的HTML吗? – SativaNL 2012-04-18 12:02:05
制作一个简化的测试案例http://css-tricks.com/reduced-test-cases/这是自行解决它的最简单方法。 – Sparky 2012-04-18 12:02:32