Guys,
I'm new to ajax and have the following code, which is supposed to populate a sub-category dropdown when a category is selected (changed) in a category dropdown. When index.php is executed, it does not post to download.php or produce an alert box. I have tested download.php, which produces the correct output if supplied with a category number. I suspect that there is a syntax error in index.php. However, I can not find it. Both index.php and download.php reside in the same directory on the server.
Any help would be appreciated.
index.php
<?php
include_once("connect.php");
?>
<html>
<head>
<title>ajax</title>
<script src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#category").change(function(){
var category_id = $(this).val();
$.ajax({
type:"post",
url:"dropdown.php",
data:"category_id="+category_id,
success: function(data) {
$("#subcategory").html(data);
}
.fail(function() { alert('error'); })
.always(function() { alert('complete'); })
});
});
});
</script>
</head>
<body>
<select id="category" name="category">
<option>--Select Category--</option>
<?php
$sql = "SELECT * FROM categories";
$result = mysqli_query($DB2_AR['DB_CONNECTION'], $sql);
if($result === FALSE) mysqli_error($DB2_AR['DB_CONNECTION']);
if(empty($result)){
echo("<option value='0'>*Cat Load Error*</option>");
mysqli_close($DB2_AR['DB_CONNECTION']);
exit;
}
while ($row_ar = mysqli_fetch_assoc($result)){
$row_ar = array_map("stripslashes", $row_ar);
echo("<option value='" . $row_ar['category_id'] . "'>" . $row_ar['category_name'] . "</option>");
}
?>
</select>
<br/>
<select id="subcategory" name="subcategory">
<option>--Select Subcategory--</option>
</select>
</body>
</html>
dropdown.php
<?php
include_once("connect.php");
$category_id = $_POST['category_id'];
$query = "SELECT * FROM subcategories WHERE category_id = '" . $category_id . "'";
$result = mysqli_query($DB2_AR['DB_CONNECTION'], $query);
if(empty($result)){
echo("<option value='0'>*Sub-Cat Load Error*</option>");
mysqli_close($DB2_AR['DB_CONNECTION']);
exit;
}
while ($row_ar = mysqli_fetch_assoc($result)){
echo("<option value='" . $row_ar['subcategory_id'] . "'>" . $row_ar['subcategory_name'] . "</option>");
}
mysqli_close($DB2_AR['DB_CONNECTION']);
?>