This question already has an answer here:
- How to populate second dropdown based on selection of first dropdown using jQuery/AJAX and PHP/MySQL? 1 answer
- How to dynamically populate options on dropdown lists based on selection in another dropdown? 1 answer
I am working on this for few days and could not find a final solution. I went through similar SOF questions, but do not know why cannot I figure this out.
I have three dropdowns. All values (options) are retrieved from a table called loan master.
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)
When a user selects one option from the first dropdown ONLY the relevant options (not all) must be loaded into second and third dropdowns
Below is the code that I have attempted. I first try to make it for the first two dropdowns.
<?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);
}
?>
I really need make this code to work. Any advice is highly appreciated
P:S Errors that I get
Query failed:Unknown column 'Array' in 'where clause' Last query performed:SELECT * FROM loan_master WHERE id=Array
Notice: Array to string conversion in C:\wamp\www\loan_mgmt\admin\getter.php on line 68
Further updates
getter.php is this file. (same file which has a php block after HTML). I have changed the code as you said.
<?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>";
}
}
?>
This is success part
success: function(res) {
$("#update").html("<option>"+res+"</option>");//options are added to second dropdwon
}
I don't get any errors now. What happens is second dropdown is empty. No options are shown