用php mysql ajax动态更新选择框 [英] dynamically updating select boxes with php mysql ajax
问题描述
我正在尝试通过php通过mysql的结果填充初始选择框.然后,我希望第二个选择框使用与第一个选择的框有关的附加信息进行更新.
I am trying to populate an initial select box with results from mysql via php. Then I would like the second select box to update with additional information related to what was chosen in the first box.
在这里,我要选择一些广告系列名称,然后在第二个框中,我要更新存储在mysql中的广告系列的版本.
Here I am selecting some campaign names, then in the second box i would like to update with the versions of the campaign stored in mysql.
这是名称脚本:
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"> </script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#campaign").change(function(){
var campaign = $("#campaign").val();
$.ajax({
type:"post",
url:"getversion.php",
data:"campaign="+campaign,
success: function(data) {
$("#version").html(data);
}
});
});
});
</script>
</head>
<body>
Campaign :
<select name="campaign" id="campaign">
<option>-Select a Campaign-</option>
<?php
include "db_conn.php";
$result = mysql_query("SELECT campaign, time FROM dfa_data GROUP BY campaign");
while($row = mysql_fetch_array($result)){
echo "<option value=$row[campaign]>$row[campaign]</option>";
} ?>
</select>
Version :
<select name="version" id="version">
<option>-Select a Version-</option>
</select>
</body>
</html>
然后,还有另一个脚本会提取第二个选择框数据,尽管它不会填充并且我也不知道为什么.
then there is another script that pulls in the second select box data, although it does not populate and I do not have any idea why.
<?php
include "db_conn.php";
$campaign = $_POST["campaign"];
$result = mysql_query("SELECT * FROM dfa_data where campaign='$campaign' GROUP BY time");
while($rowa = mysql_fetch_array($result)){
echo"<option value=$rows[time]>$rows[time]</option>";
}
?>
任何人都可以告诉我我做错了什么,为什么第二个选择框不会填充.预先感谢.
Can anyone show me what I am doing wrong and why the second select box will not populate. Thanks in advance.
推荐答案
不确定这是否是您的问题,但可能是一个问题.在第二个脚本中,您有:
Not sure if this is your issue, but it's probably AN issue. In your second script you have:
while($rowa = mysql_fetch_array($result)){
echo"<option value=$rows[time]>$rows[time]</option>";
}
您正在获取 $ rowa
,但尝试访问 $ rows
.试试这个吧.
You are fetching into $rowa
, but trying to access $rows
. Try this instead.
while($row = mysql_fetch_array($result)){
echo '<option value="'.$row['time'].'">'.$row['time'].'</option>';
}
这篇关于用php mysql ajax动态更新选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!