i'm parsing a csv file with ajax and php, although i think i did everything right, nothing happens when i click on the link.

here is the code:

here is the html:
<div id="brandContainer">
<ul id="brandNav">
<li class="categorypage" id="face"><a href="javascript:showCD('a','brandPage')">A</a></li>
</ul>
<div id="brandPage"></div>
</div>

here is the ajax/javascript:

var xmlhttp

function showCD(str, div)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="getbrands.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged(div);
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged(div)
{
if (xmlhttp.readyState==4)
{
document.getElementById(div).innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}



and here is the php(getbrands.php):

<?php

$q=$_GET["q"];

$handle = fopen("http://www.totalbeauty.com/resource/lists", "r");
while (($data = fgetcsv($handle, ";;")) !== FALSE) {
	$brands= $data[0];
    }
$brand = explode(",", $brands);
for ($i=0; $i<=count($brand); $i++){
	
	$a=strtolower($brand[$i]);
	$display = "<ul>";
	if ($q = $a[0]){
		$display .= "<li>'".$a."'</li>";
	}
	$display .="</ul>";
	echo $display;
}


?>

can anyone please tell me what is wrong with this code?
thanks...

If you get an error make sure it is not an access denied. An access denied is pretty common if your on a windows server. Because your requesting xmlhttp.open("GET",url,true); and windows is funny about those things.

Try this:

here is the ajax/javascript

var xmlhttp;
function showCD(str, div)
{

xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }
var url="getbrands.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function stateChanged(div)
{
if (xmlhttp.readyState==4)
{
document.getElementById(div).innerHTML=xmlhttp.responseText;
}
}

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

here is the html

<div id="brandContainer">
<ul id="brandNav">
<li class="categorypage" id="face"><a href="javascript:showCD

('a','brandPage')">A</a></li>
</ul>
<div id="brandPage"></div>
</div>


and here is the php(getbrands.php)

<?php

$q=$_GET["q"];

$handle = fopen("http://www.totalbeauty.com/resource/lists", "r");
while (($data = fgetcsv($handle, ";;")) !== FALSE) {
	$brands= $data[0];
    }
$brand = explode(",", $brands);
for ($i=0; $i<=count($brand); $i++){
	
	$a=strtolower($brand[$i]);
	$display = "<ul>";
	if ($q = $a[0]){
		$display .= "<li>'".$a."'</li>";
	}
	$display .="</ul>";
	echo $display;
}


?>
This article has been dead for over six months. Start a new discussion instead.