Clearly, I’m doing something wrong but I’ve been working on this all day and for some reason can’t get it to work. I’ve gone through http://api.jquery.com/jQuery.post/ backwards and forwards, any help is greatly appreciated!
jQuery:
$(function () {
$('#vehicleModel').change(function() {
$.post("ajax.php", { vehicleModel:$('#vehicleModel').val() },
function(data){
alert(data.optionValue);
$('#vehicleYear option').remove();
$.each(data,function(i,item) {
$('#vehicleYear').append('<option value="'+item.optionValue+'">'+item.optionDisplay+'</option>');
});
}, "json");
});
});
PHP:
$Model_t = $_REQUEST['vehicleModel'];
$results = mysql_query('select * from vehicle_applications where Model_t="' . $Model_t . '"');
$json = array();
while (is_resource($results) && $row = mysql_fetch_object($results)) {
$Year = strtoupper($row->Year_t);
$Model = strtoupper($row->Model_t);
$json[] = '{optionValue: "'.$Model.'", optionDisplay: "'.$Year.'"}';
}
header('Content-type: application/json');
echo '[' . implode(',', $json) . ']';
die();
My response in firebug looks like:
[{optionValue: "FORD CAR", optionDisplay: "41-48"},{optionValue: "FORD CAR", optionDisplay: "32"},{optionValue: "FORD CAR", optionDisplay: "33-34"}]
I get nuthin’ from the alert but firebug says the json n/v is there.