I am having an issue with ajax/php. When the page loads in firefox I get a mesage box that says "the url can not be loaded" The page has 2 select boxes, the first one is categories, the second item. When the category is selected, the options in the item one are updated to only items in that category and all items fitting that category are displayed on the page in a diffirent div. the second select box limits the list to the specific item selected. A sample of the page is at
http://www.talebinc.net/zampell. If someone could take a look at my code and maybe see where I may have gone wrong it would be great, thanks.
here is the code for the index.php:
include('header.php');
include('dbconnect.php')
?>
Select a Category:
--SELECT--
$sql="SELECT * FROM categories";
$result= mysql_query($sql);
while ($row = mysql_fetch_array($result)){
$cat = $row['category'];
$num = $row['id'];
echo " ".$cat." ";
}
?>
Select a description
$sql="SELECT Item_number, description FROM inventory order by description";
$result= mysql_query($sql);
echo " --SELECT-- ";
while ($row = mysql_fetch_array($result)){
$desc = $row['description'];
$itemnum =$row['Item_number'];
echo " ".$desc." ";
}
mysql_close();
?>
Enter the Item Number:
Results will appear here
?>
----------
and the ajax code is here:
var xmlhttp;
var xmlhttp2;
function showCat(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getcat.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(null);
showTable(str);
}
function showTable(str)
{
xmlhttp2=GetXmlHttpObject();
if (xmlhttp2==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="showtable.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp2.onreadystatechange=resultstable;
xmlhttp2.open("GET",url,true);
xmlhttp2.send(null);
}
function resultstable()
{
if (xmlhttp2.readyState==4)
{
document.getElementById("displayresults").innerHTML=xmlhttp2.responseText;
}
}
function stateChanged()
{
if (xmlhttp.readyState==4)
{
document.getElementById("txtHint").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;
}
function showdesc(str)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
var url="getnumber.php";
url=url+"?q="+str;
url=url+"&sid="+Math.random();
xmlhttp.open("GET",url,true);
xmlhttp.onreadystatechange=stateChangeDesc;
xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xmlhttp.send(null);
showTable(str);
}
function stateChangeDesc()
{
if (xmlhttp.readyState==4)
{
document.getElementById("number").innerHTML=xmlhttp.responseText;
}
}
------------------------------------------------------------------------
and the getcat.php file:
$q = $_GET['q'];
//echo $
include ('scripts/dbconnect.php');
$sql="SELECT Item_number, description FROM inventory WHERE category like '".$q."%' ORDER by description";
$result = mysql_query($sql);
echo 'Select a description: ';
echo " --SELECT-- ";
while ($row = mysql_fetch_array($result)){
$itemnum = $row['Item_number'];
$desc = $row['description'];
echo " ".$desc." ";
}
echo " ";
mysql_close();
?>
-----------------------------------------------------------------
Add New Comment