尝试从 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>