【发布时间】:2014-04-28 16:28:11
【问题描述】:
我为此工作了几天,但找不到最终解决方案。我经历了类似的 SOF 问题,但不知道为什么我无法弄清楚。
我有三个下拉菜单。所有值(选项)都从一个称为贷款主表的表中检索。
First dropdown -> Loan type (values such as housing,personal,instant are in the table).
Second dropdown -> Loan amount (different amounts have been defined for each type)
Third dropdwon - > Interest Rate (same as second)
当用户从第一个下拉列表中选择一个选项时,只有相关选项(不是全部)必须加载到第二个和第三个下拉列表中
以下是我尝试过的代码。我首先尝试为前两个下拉菜单制作它。
<?php
require_once "../includes/loan_master.php";
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Basic</title>
</head>
<body>
Loan Type <select id="first" name="loan_master_id" class="textInput">
<!--first dropdwon-->
<?php
$loans=$loan_master->find_by_sql("SELECT * FROM loan_master");
//this will return an array
foreach ($loans as $loan) {
echo "<option value='$loan->id'>$loan->loan_type</option> ";
}
?>
</select>
Loan Amount<select id="update">
<!--second dropdown-->
</select>
<script src="../javascripts/jquery-1.11.0.js"></script>
<script>
$(function() {
$("#first").change(function() {
var x = $("#first").val();
// first dropdown value is stored
fire_ajax(x);
});
function fire_ajax() {
$.ajax({
type: "POST",
dataType: "text",//is this data type corect?
url: "getter.php",
//getter.php is this file. All are in the same file
success: function(res) {
$("#update").html("<option>"+res+"</option>");//options are added to second dropdwon
}
})
}
});
</script>
</body>
</html>
<?php
//php code
if (isset($_POST)) {
$x=$_POST; // I feel something is wrong here
$loans=$loan_master->find_by_sql("SELECT * FROM loan_master WHERE id=".$x);
}
?>
我真的需要让这段代码工作。任何建议都非常感谢
P:S 我得到的错误
查询失败:“where 子句”中的未知列“数组” 上次执行的查询:SELECT * FROM loan_master WHERE id=Array
注意:第 68 行 C:\wamp\www\loan_mgmt\admin\getter.php 中的数组到字符串转换
进一步更新
getter.php 就是这个文件。 (同一个文件,在 HTML 之后有一个 php 块)。正如你所说,我已经更改了代码。
<?php
if (isset($_POST["loan_master_id"])) {
$x=$_POST["loan_master_id"]; // I feel something is wrong here
$loans=$loan_master->find_by_sql("SELECT loan_amount FROM loan_master WHERE id=".$x);
foreach ($loans as $$loan) {
echo "<option value='$loan->id'>$loan->loan_amount</option>";
}
}
?>
这是成功的部分
success: function(res) {
$("#update").html("<option>"+res+"</option>");//options are added to second dropdwon
}
我现在没有收到任何错误。发生的情况是第二个下拉列表为空。未显示任何选项
【问题讨论】:
-
三个下拉菜单共有多少个选项?我问是因为如果它只有几个(比如说
-
@Daniel Gimenez - 选项不是静态的。在贷款主表中,管理员可以在未来添加一个名为“ABC”的贷款类型并定义其金额和利率。
-
我并不是真正的 Ajax 专家,所以有时我无法理解可能为类似问题提供的答案。
-
这里提供的答案也可能不容易理解。您无需成为 AJAX 方面的专家,您只需了解它的工作原理即可。
-
@Jay- 是的,我尽力理解了一些,这就是我想出自己的答案的方式,我仍在尽我所能
标签: javascript php jquery ajax drop-down-menu