i want when a category is selected, the user only gets the product of that paticular category.

here is my code
ajax_listbox.php

<html> <head> <title>Demo of List box selection and displaying matching records</title> <script type="text/javascript">
function ajaxFunction()
{

//document.writeln(val)
var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }

function stateChanged(){
    if(httpxml.readyState==4){

var myObject = JSON.parse(httpxml.responseText);  // Received the data 

var msg=myObject.value.message;
if(msg.length > 0){document.getElementById("msg").innerHTML=msg;}
else{document.getElementById("msg").style.display='none';}

var str="<table width='50%'  align=center><tr><th align='left' bgcolor='#ffff00'>ID</th><th align='left' bgcolor='#ffff00'>Name</th></tr>";
var color="#f1f1f1";
for(i=0;i<myObject.data.length;i++)
{ 
if((i%2)==0){color="#ffffff";}
else{color="#f1f1f1";}
str = str + "<tr bgcolor="+color+"><td>" + myObject.data[i].cat_id + " </td><td>"+ myObject.data[i].cat_name + "</a></td></tr>"
}
str = str + "</table>" ;
document.getElementById("display").innerHTML=str;
    }
    }

var url="ajax-listbox-demo2.php";
var id=document.myForm.id.value;
url=url+"?id="+id;
url=url+"&kid="+Math.random();
//alert(url)
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);

document.getElementById("msg").style.background='#f1f1f1';
document.getElementById("msg").innerHTML="Please Wait ... ";
document.getElementById("msg").style.display='inline';
}

</script> <?php
//////////////////////////// Your Database details here /////////////////
$bdd = new PDO('mysql:host=127.0.0.1; dbname=doc_booking', 'root',''); // Database Connection 
///////////////////////////

echo "</head><body onload=\"ajaxFunction()\";>";
echo "<center><table border='0' width='100%' cellspacing='0' cellpadding='0' > <tr bgcolor='#ffffcc'><form  name=myForm method='post' onSubmit=\"ajaxFunction(this.form); return false\"> <td align='center' colspan=2><font face='verdana, arial, helvetica' size='2'><b> Select a Category</b></font></td></tr>";
echo "<tr>";
echo "<td  align='center'>";
$query="SELECT * FROM  category ORDER BY cat_name";

echo "<select name=id onChange=\"ajaxFunction()\"><option value=0>Show All</option>";
foreach ($bdd->query($query) as $nt) {
echo "<option value=$nt[id]>$nt[cat_name]</option>";
}
echo "</select>";
echo "</font></td>";
echo "</tr></form>";
echo "</table>";


?> <div id=msg style="position:absolute;  z-index:1; left: 1100px; top: 0px;" >This is message area</div> <div id="display"><b>Records will be displayed here</b></div> </body> </html>

///////////////////////////////////////////////////////end of the first file///////////////////////////////////////////////////

and ajax_listbox_2.php

<?php
$bdd = new PDO('mysql:host=127.0.0.1; dbname=doc_booking', 'root','');  // Database Connection 
///////////////////////////
//////////////////////////////// Main Code sarts /////////////////////////////////////////////
@$cat_id=$_GET['id'];
//$cat_id=1;

if(!is_numeric($cat_id)){
$message.="Data Error |";
exit;
}

if($cat_id>0){
$sql="select lastname, firstname from doctors where cat_id=$cat_id order by cat_name";
}else{
$sql="select lastname, firstname from doctors order by cat_name ";
$cat_id=0;
}
//////// collecting data from table ////////
$row=$bdd->prepare($sql);
$row->execute();
$result=$row->fetchAll(PDO::FETCH_ASSOC);
//////////////
@$main = array('data'=>$result,'value'=>array("cat_id"=>"$cat_id","message"=>"$message"));
echo json_encode($main);  // Json string returned 

?> 

You haven't mentioned what problem you are having. Does the PHP script work by itself when you test it?
Does the AJAX request connect to it correctly? What exactly is the problem?

Also, you would benefit from using something newer like jQuery. I haven't seen that XMLHttpRequest code in years.

Really need to see what your tables look like.
If Category is a seperate table with ID and Name and the doctors table only has Cat_ID not Cat_Name then your query is not joining to the category table to get the category name.

Just a guess....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.