I have the following code which should update the total items in a categories list every time that a search filter is changed. However, the function will only run once.
function getXMLHTTP() { //fuction to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCategoryTotals(temp) {
var state = document.getElementById('state').value;
var area = document.getElementById('area').value;
var type = document.getElementById('type').value;
var category = document.getElementById('category').value;
var strURL="<?= $path ?>common/locationSelector/findCategoryTotals.php?state="+state+"&area="+area+"&type="+type+"&category="+category;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('categorydiv').innerHTML=req.responseText;
} else {
document.getElementById('categorydiv').innerHTML=req.responseText;
alert("There was a problem while using XMLHTTP:\n" + req.responseText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
Here is where it is being called and used:
http://ground360.org/coupons/index.php
The PHP file* it calls upon contains the following:
$type = secure($_GET['type']);
$state = secure($_GET['state']);
$area = secure($_GET['area']);
$selectedCategory = secure($_GET['category']);
$categories = array('From Airport', 'To Airport', 'Hourly', 'One-way');
echo "<select name=\"category\">
<option value=\"\">Select Category</option>
<optgroup label=\"Most Popular:\">";
foreach ($categories as $category) {
$countQuery = "SELECT id FROM gd_offers WHERE category = '$category' AND expirationDate >= NOW() AND status = 'Approved'";
if ($state != '') {
$countQuery .= " AND memberID IN (
SELECT id
FROM members
WHERE company IN (
SELECT tcid FROM tcomps
WHERE astate IN (
SELECT sab
FROM state_ft
WHERE stateid = '$state') ";
if ($area != '') {
$countQuery .= " AND tcid IN (
SELECT companyid FROM area_company WHERE areaid = '$area')";
}
$countQuery .= "))";
}
$countResult = mysql_query($countQuery);
echo "<option value=\"$category\"";
if ($selectedCategory == $category) {
echo " selected";
}
echo ">$category (" . mysql_num_rows($countResult) . ")</option>";
}
echo "</optgroup>
<optgroup label=\"Special Occasions:\">";
$query = "SELECT * FROM gd_categories WHERE category != 'To Airport' AND category != 'From Airport' AND category != 'Hourly' AND category != 'One-way' ORDER BY category ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$countQuery = "SELECT id FROM gd_offers WHERE category = '" . $row['category'] . "' AND expirationDate >= NOW() AND status = 'Approved'";
if ($state != '') {
$countQuery .= " AND memberID IN (
SELECT id
FROM members
WHERE company IN (
SELECT tcid FROM tcomps
WHERE astate IN (
SELECT sab
FROM state_ft
WHERE stateid = '$state') ";
if ($area != '') {
$countQuery .= " AND tcid IN (
SELECT companyid FROM area_company WHERE areaid = '$area')";
}
$countQuery .= "))";
}
$countResult = mysql_query($countQuery);
echo "<option value=\"" . $row['category'] . "\"";
if ($selectedCategory == $row['category']) {
echo " selected";
}
echo ">" . $row['category'] . " (" . mysql_num_rows($countResult) . ")</option>";
}
echo "</optgroup>
</select>";
*A database connection is present, I just left the connect/disconnect out.