已经解决了,谢谢你< 3
我遇到问题,当我尝试使用上一个下拉列表中选择的列中的数据库中的数据填充第二个下拉列表时,例如:
ddl:DDL1 ddl:DDL2按钮:搜索
在DDL1中选择时,例如ID,DDL2应该允许您从数据库中选择一个现有ID。
这是我的HTML表单:
<form id="detailed_search_form">
<select id="select_columns" name="select_columns" onchange="take_value()">
</select>
<select id="select_value" name="select_value">
</select>
<button id="button_search" value="search" onclick="search()">Search</button>
</form>
<div id="fill_me"></div>
这填补了DDL1,而且这个工作:
function take_columns(){
var tab = new Array();
$.ajax({
dataType: "json",
url: "take_columns.php",
data: { },
success: function(data) {
for (var i=0;i<data.length;i++){
tab[i]=data[i]["Column_name"];
}
var $el = $("#select_columns");
$el.empty(); // remove old options
for (var i=0;i<tab.length;i++){
$el.append($("<option></option>").attr("value", tab[i]).text(tab[i]));
}
}
});
};
这应该填写DDL2并且这不起作用:
function take_value() {
var e = document.getElementById("select_columns");
var requestedColumn = e.options[e.selectedIndex].value;
$.ajax({
data: { column: requestedColumn },
url: 'take_value.php',
dataType: 'json',
success: function(json) {
for (var i = 0;i<data.length;i++){
tab[i]=data[i][requestedColumn];
alert(tab[i]);
}
var $el = $("#select_value");
$el.empty(); // remove old options
for (var i=0;i<tab.length;i++){
$el.append($("<option></option>").attr("value", tab[i]).text(tab[i]));
}
}
});
};
这是take_value.php:
$pdo = new PDO($dsn, $username, $password);
if(isset($_GET['column'])) {
$whatColumn = $_GET['column'];
$stmt = $pdo->prepare("SELECT $whatColumn FROM kandydaci");
$stmt->bindParam(':column', $whatColumn, PDO::PARAM_STR);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
}
echo json_encode($rows);