Reputation: 51
Here i tried as much i can but couldn't get results. please any guys help me out. i cannot get the correct result using these codes.may be something i'm missing.
$get_medical_aod_name = $_POST['name'];
$select_query = mysql_query("SELECT id, name FROM insurance_companies WHERE cat=$get_medical_aod_name");
while ($row = mysql_fetch_array($select_query)) {
$result[] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
header('Content-Type: application/json', true);
echo json_encode($result);
and in my script.js
$('#medical_aid_name').change(function(){
//alert("working");
var name = $(this).val();
$.post('ins_bene.php',
{'name' : name },
function(data){
var select = $('#benefit').empty();
$.each(data.values, function(i,data) {
select.append( '<option value="'
+ data.id
+ '">'
+ data.name
+ '</option>' );
});
}, "json");
});
and i feel everything fine but i'm getting no output :(
Upvotes: 1
Views: 2538
Reputation: 891
Change this in script.js
$.each(data.values, function(key, value)
to
$.each(data, function(key, value) {
OR
Assign a key values
to $result
while ($row = mysql_fetch_array($select_query)) {
$result['values'] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
Upvotes: 0
Reputation: 9596
this should work
while ($row = mysql_fetch_array($select_query)) {
$result['values'] = array(
'id' => $row['id'],
'name' => $row['name']
);
}
Upvotes: 0
Reputation:
Instead of the following code:
var select = $('#benefit').empty();
$.each(data.values, function(i,data) {
select.append( '<option value="'
+ data.id
+ '">'
+ data.name
+ '</option>' );
});
Use the below code:
var select = $('#benefit').empty();
$.each(data.values, function(i,data) {
$('#benefit').append( '<option value="'
+ data[i].id
+ '">'
+ data[i].name
+ '</option>' );
});
Here select var don't have the object of the select tag that's why.
Upvotes: 0
Reputation: 1239
Try this... UPDATED
$('#medical_aid_name').change(function() {
var name = $(this).val();
$.post('ins_bene.php', { 'name': name },
function(data) {
alert(data) // [object][object],[object][object]
$('#benefit').empty();
$.each(data, function(key, value) {
$('#benefit').append('<option value="' + value.id + '">' + value.name + '</option>');
});
}, "json");
});
Upvotes: 1
Reputation: 3337
You need to quote the query:
$select_query = mysql_query("SELECT id, name FROM insurance_companies WHERE cat=$get_medical_aod_name");
to
$select_query = mysql_query('SELECT id, name FROM insurance_companies WHERE cat="' . mysql_real_escape_string($get_medical_aod_name) .'"');
Please don't use mysql_ functions. Move to PDO!
Upvotes: 1