Location: PHPKode > scripts > AJAX Drop-down > getagents.php
<?php
//  
// +------------------------------------------------------------------------+
// | PHP version 5.0 					                                  	|
// +------------------------------------------------------------------------+
// | Description:													      	|
// | Class to populate drop down using AJAX + PHP 	  						|	
// | 																		|	
// +------------------------------------------------------------------------+
// | Author				: Neeraj Thakur <hide@address.com>   			|
// | Created Date     	: 18-12-2006                  						|
// | Last Modified    	: 18-12-2006                  						|
// | Last Modified By 	: Neeraj Thakur                  					|
// +------------------------------------------------------------------------+


DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'testDB');

class AjaxDropdown
{
	var $table;
	
	function AjaxDropdown()
	{		
		// Make the connnection and then select the database.
		$dbc = @mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die ('Could not connect to MySQL: ' . mysql_error() );
		mysql_select_db (DB_NAME) OR die ('Could not select the database: ' . mysql_error() );
		$this->table = "tblcategories";
	}
	
	function dbConnect()
	{
		DEFINE ('LINK', mysql_connect (DB_HOST, DB_USER, DB_PASSWORD));
	}
	
	function getXML($id)
	{
		$this->dbConnect();
		$query = "SELECT * FROM $this->table where pid = {$id} ORDER BY id asc";
		$result = mysql_db_query (DB_NAME, $query, LINK);
		
		$xml = '<?xml version="1.0" encoding="ISO-8859-1" ?>';
		$xml .= '<categories>';
		while($row = mysql_fetch_array($result))
		{
			$xml .= '<category>';
			$xml .= '<id>'. $row['id'] .'</id>';
			$xml .= '<fname>'. $row['Name'] .'</fname>';
			$xml .= '</category>';
		}
		$xml .= '</categories>';
		mysql_close();		
		return $xml;
	}	
	
	function getArray($id)
	{
		$this->dbConnect();
		$query = "SELECT * FROM $this->table where pid = {$id} ORDER BY id asc";
		$result = mysql_db_query (DB_NAME, $query, LINK);
		$arr = array();
		while($row = mysql_fetch_object($result))
		{
			$arr[] = $row;
		}
		mysql_close();		
		return $arr;	
	}
}

if ( @$_GET['method'] == 'getXML' )
{
	header("Content-Type: application/xml; charset=UTF-8");
	$obj = new AjaxDropdown();
	echo $obj->getXML(@$_GET['param']);
}
?>
Return current item: AJAX Drop-down