I am loading a list of customers and I want to load a second dropdown for locations based on which customer gets selected. For some reason my code isn't working. Here's my code:
<span style="display:inline-block;">
<select name="sCustomer" id="sCustomer" onChange="findLocations(this.value)">
<option value="0">- Select Customer -</option>
</select>
</span>
<span style="display:inline-block;">
<select id="sLocation" name="sLocation">
<option value="0">- Select Location -</option>
</select>
</span>
<script type="text/javascript">
function findLocations(custID) {
$('#sLocation').empty();
$('#sLocation').append("<option value='0'>- Select Location -</option>");
$.ajax({
type:"POST",
url:"getLocations.php",
contentType:"application/json; charset=utf-8",
data: { custID : custID },
dataType:"json",
success: function(data){
$.each(data,function(i, item){
$('#iTest').val("bobby");
$('#sLocation').empty();
$('#sLocation').append("<option value='0'>- Select Location -</option>");
$('#sLocation').append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
});
},
complete: function(){
}
});
}
</script>
and the page it calls has this code
<?php
include_once(INCLUDE_DIR.'class.error.php');
include_once(INCLUDE_DIR.'class.role.php');
include_once(INCLUDE_DIR.'class.passwd.php');
include_once(INCLUDE_DIR.'class.user.php');
include_once(INCLUDE_DIR.'class.auth.php');
include_once(INCLUDE_DIR.'class.location.php');
function getLocations() {
$custID = 0;
if (isset($_POST['custID'])) {
$custID = $_POST['custID'];
}
$sql = 'SELECT l.location_id, l.site_name FROM ' . CUST_LOCATION_TABLE
. ' AS l JOIN ' . CUST_TABLE . ' AS c ON c.cust_id=l.cust_id'
. ' WHERE c.cust_id='.$custID;
$locations = array();
if (($res=db_query($sql)) && db_num_rows($res)) {
while(list($id,$name)=db_fetch_row($res)) {
$columns = array (
'locID' => $id,
'locName' => $name,
);
$locations[] = $columns;
}
}
return $locations;
}
?>
Why isn't this working and is there anyway to test what part is breaking? I can't echo out anything because the page doesn't postback (since it's ajax) and I can't do javascript alert in the ajax function. Bear in mind I didn't add the code that was filling the customer dropdown because I know it's working right, populating everything and giving me the correct values.
Okay I found this which at least helps me start debugging. It's failing but I found this
error: function (request, error) {
console.log(arguments);
alert(" Can't do because: " + error);
},
echo json_encode(getLocations());for the json to be sent