Ajax Example
1.create database table for country and state
CREATE TABLE /*!32312 IF NOT EXISTS*/ `country_master` (
`country_id` int(20) NOT NULL auto_increment,
`country_name` varchar(50) default NULL,
`country_lastupdate` date default NULL,
`admin_id` varchar(20) default NULL,
`country_status` int(2) default NULL,
PRIMARY KEY (`country_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
#
# Dumping data for table ‘country_master’
#
/*!40000 ALTER TABLE country_master DISABLE KEYS;*/
LOCK TABLES country_master WRITE;
REPLACE INTO country_master (country_id, country_name, country_lastupdate, admin_id, country_status) VALUES (1,’India’,NULL,NULL,1),
(2,’United States’,NULL,NULL,1);
UNLOCK TABLES;
/*!40000 ALTER TABLE country_master ENABLE KEYS;*/
CREATE TABLE /*!32312 IF NOT EXISTS*/ `state_master` (
`State_id` int(20) NOT NULL auto_increment,
`country_id` int(20) default NULL,
`state_name` varchar(50) default NULL,
`state_lastupdate` date default NULL,
`admin_id` varchar(20) default NULL,
`Status` int(2) default NULL,
PRIMARY KEY (`State_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
#
# Dumping data for table ‘state_master’
#
/*!40000 ALTER TABLE state_master DISABLE KEYS;*/
LOCK TABLES state_master WRITE;
REPLACE INTO state_master (State_id, country_id, state_name, state_lastupdate, admin_id, `Status`) VALUES (1,1,’Gujrat’,NULL,NULL,1);
UNLOCK TABLES;
/*!40000 ALTER TABLE state_master ENABLE KEYS;*/
2.create php page
–First Make Databse connection
define("NAME","country"); //your database name
define("USER",'root');
define("PASSWORD",’’);
define("HOST","localhost");
//blank cache memmory
header("Cache-control: private, no-cache");
header("Pragma: no-cache");
$prefix="way";
$link = mysql_connect(HOST,USER,PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(NAME,$link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
|
* Country
Name : |
||
|
*
State Name : |
–Add following JavaScript function in
tagStep -3 Create Ajax output page(ajx_getstatenm.php).
define("NAME","country"); //your database name
define("USER",'root');
define("PASSWORD",’’);
define("HOST","localhost");
//blank cache memmory
header("Cache-control: private, no-cache");
header("Pragma: no-cache");
$prefix="way";
$link = mysql_connect(HOST,USER,PASSWORD);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db(NAME,$link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
//*********************************************************AJAX*******************************************************************
?>
//*******************************************For Insert Data In Database*****************************************
if($_GET["country_id"]!="" )
{
$id=$_GET["country_id"];
$stateid=$_GET["State_id"];
$sql = "select * from state_master where country_id=$id and Status=1";
$res = mysql_query($sql);
$total = mysql_num_rows($res);
if ($total > 0)
{
$topstate = “215″;
?>