SlideShare a Scribd company logo
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
Country State City in Dropdown list in PHP using Ajax
First of all we create a database then we will create three tables in databse i.e. country ,
state and city table. Like as following images.
Country Table
CREATE TABLE `country` (
`Country_Id` varchar(255) NOT NULL,
`CountryName` varchar(255) NOT NULL
);
INSERT INTO `country` (`Country_Id`, `CountryName`) VALUES
('1', 'India'),
('2', 'Usa'),
('3', 'Canada');
State Table
CREATE TABLE `state` (
`StateId` varchar(255) NOT NULL,
`Country_Id` int(11) NOT NULL,
`StateName` varchar(255) NOT NULL
);
INSERT INTO `state` (`StateId`, `Country_Id`, `StateName`) VALUES
('1', 1, 'Orissa'),
('2', 1, 'Andhra Pradesh'),
('3', 1, 'West Bengal'),
('4', 1, 'Maharastra'),
('5', 2, 'Arizona'),
('6', 2, 'California'),
('7', 2, 'Texas'),
('8', 3, 'Alberta'),
('9', 3, 'Ontario');
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
City Table
CREATE TABLE `city` (
`CityId` varchar(255) NOT NULL,
`State_Id` int(11) NOT NULL,
`CityName` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
INSERT INTO `city` (`CityId`, `State_Id`, `CityName`) VALUES
('1', 1, 'Bhubaneswar'),
('2', 2, 'Hyderbad'),
('3', 3, 'Kolkata'),
('4', 4, 'Mumbai'),
('5', 5, 'Calgary'),
('6', 5, 'Edmonton'),
('7', 5, 'Duffield'),
('8', 6, 'Phoenix'),
('9', 7, 'San Diego'),
('10', 8, 'Austin'),
('11', 9, 'Toronto');
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
Now we will create configuration file in php i.e. config.php. I which file we will create the
database connectivity.
config.php file
<?php
$host="localhost";
$username="root";
$password="";
$dbname="country_dropdown";
$con=mysql_connect("$host","$username","$password");
mysql_select_db("$dbname",$con);
?>
Now we will create index.php
Index.php
<?php
include('config.php');
$Country_query="select id,country_name from country order by country_name asc ";
$Country=mysql_query($Country_query);
$Count_Country=mysql_num_rows($Country);
?>
<html>
<head>
<title>Country State City Drop Down Using Ajax in PHP</title>
<!--<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"
type="text/javascript">
<script type="text/javascript">
function selectCity(country_id){
if(country_id!="-1"){
loadData('state',country_id);
$("#city_dropdown").html("<option value='-1'>Select city</option>");
}else{
$("#state_dropdown").html("<option value='-1'>Select state</option>");
$("#city_dropdown").html("<option value='-1'>Select city</option>");
}
}
function selectState(state_id){
if(state_id!="-1"){
loadData('city',state_id);
}else{
$("#city_dropdown").html("<option value='-1'>Select city</option>")
}
}
function loadData(loadType,loadId){
var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
$.ajax({
type: "POST",
url: " load_state_city.php",
data: dataString,
cache: false,
success: function(result){
$("#"+loadType+"_loader").hide();
$("#"+loadType+"_dropdown").html("<option value='-1'>Select
+loadType+"</option>");
$("#"+loadType+"_dropdown").append(result);
}
});
}
</script>
</head>
<body>
<?php
if($Count_Country > 0){
?>
<table>
<tr>
<td>Country:</td>
<td>
<select onchange="selectCity(this.options[this.selectedIndex].value)">
<option value="-1">Select country</option>
<?php
while($rowCountry=mysql_fetch_array($Country)){
?>
<option value="<?php echo $rowCountry['id']?>"><?php echo
$rowCountry['country_name']?></option>
<?php
}
?>
</select>
</td>
</tr>
<tr>
<td>State:</td>
<td>
<select id="state_dropdown"
onchange="selectState(this.options[this.selectedIndex].value)">
<option value="-1">Select state</option>
</select><span id="state_loader"></span>
</td>
</tr>
<tr>
<td>City:</td>
<td>
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
<select id="city_dropdown">
<option value="-1">Select city</option>
</select><span id="city_loader"></span>
</td>
</tr>
</table>
<?php
}else{
echo 'No Country Name Found';
}
?>
</body>
</html>
load_state_city.php
<?php
include('config.php');
$loadType=$_POST['loadType'];
$loadId=$_POST['loadId'];
if($loadType=="state"){
$sql="select id,state_name from state where country_id='".$loadId."' order by
state_name asc";
}else{
$sql="select id,city_name from city where state_id='".$loadId."' order by
city_name asc";
}
$res=mysql_query($sql);
$check=mysql_num_rows($res);
if($check > 0){
$HTML="";
while($row=mysql_fetch_array($res)){
$HTML.="<option value='".$row['id']."'>".$row['1']."</option>";
}
echo $HTML;
}
?>
Output
When we run the index.php file then we will get following output. Suppose we select
Canada from country dropdown then display state in state dropdown along with Canada.
When we select state from state dropdown then city will display in city dropdown along
with selected state.
PHP Tutorials By vineet Kumar Saini
www.vineetsaini.wordpress.com
.1 .2
.3 .4
Conclusion
Using this article one can get easily understand cascading dropdown list in php.

More Related Content

What's hot

Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
Vineet Kumar Saini
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
Manoj Kumar
 
Techical Workflow for a Startup
Techical Workflow for a StartupTechical Workflow for a Startup
Techical Workflow for a Startup
Sébastien Saunier
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
Bui Kiet
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
ShahDhruv21
 
Express js
Express jsExpress js
Express js
Manav Prasad
 
Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)
Peter Lubbers
 
Css3
Css3Css3
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
TheCreativedev Blog
 
Javascript
JavascriptJavascript
Javascript
Sun Technlogies
 
Java script cookies
Java script   cookiesJava script   cookies
Java script cookies
AbhishekMondal42
 
Expressjs
ExpressjsExpressjs
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
Gabriele Lana
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
Jeff Fox
 
Node.js Basics
Node.js Basics Node.js Basics
Node.js Basics
TheCreativedev Blog
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
Priya Goyal
 
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Svetlin Nakov
 
Nodejs vatsal shah
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shah
Vatsal N Shah
 

What's hot (20)

Add edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHPAdd edit delete in Codeigniter in PHP
Add edit delete in Codeigniter in PHP
 
ES6 PPT FOR 2016
ES6 PPT FOR 2016ES6 PPT FOR 2016
ES6 PPT FOR 2016
 
Techical Workflow for a Startup
Techical Workflow for a StartupTechical Workflow for a Startup
Techical Workflow for a Startup
 
JavaScript Tutorial
JavaScript  TutorialJavaScript  Tutorial
JavaScript Tutorial
 
Event In JavaScript
Event In JavaScriptEvent In JavaScript
Event In JavaScript
 
Express js
Express jsExpress js
Express js
 
Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)
 
Css3
Css3Css3
Css3
 
Node.js Express Framework
Node.js Express FrameworkNode.js Express Framework
Node.js Express Framework
 
Javascript
JavascriptJavascript
Javascript
 
Java script cookies
Java script   cookiesJava script   cookies
Java script cookies
 
Expressjs
ExpressjsExpressjs
Expressjs
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
JSON and XML
JSON and XMLJSON and XML
JSON and XML
 
Express node js
Express node jsExpress node js
Express node js
 
JSON: The Basics
JSON: The BasicsJSON: The Basics
JSON: The Basics
 
Node.js Basics
Node.js Basics Node.js Basics
Node.js Basics
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
Cryptography for Java Developers: Nakov jProfessionals (Jan 2019)
 
Nodejs vatsal shah
Nodejs vatsal shahNodejs vatsal shah
Nodejs vatsal shah
 

Viewers also liked

Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
Vineet Kumar Saini
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
Pankaj Jha
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
Vineet Kumar Saini
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
Vineet Kumar Saini
 
How to use "PENCIL" animation software
How to use "PENCIL" animation softwareHow to use "PENCIL" animation software
How to use "PENCIL" animation softwareTanja Gvozdeva
 

Viewers also liked (7)

Dropdown List in PHP
Dropdown List in PHPDropdown List in PHP
Dropdown List in PHP
 
Pagination in PHP
Pagination in PHPPagination in PHP
Pagination in PHP
 
PHP Technical Questions
PHP Technical QuestionsPHP Technical Questions
PHP Technical Questions
 
Abstract Class and Interface in PHP
Abstract Class and Interface in PHPAbstract Class and Interface in PHP
Abstract Class and Interface in PHP
 
Computer Fundamentals
Computer FundamentalsComputer Fundamentals
Computer Fundamentals
 
Implode & Explode in PHP
Implode & Explode in PHPImplode & Explode in PHP
Implode & Explode in PHP
 
How to use "PENCIL" animation software
How to use "PENCIL" animation softwareHow to use "PENCIL" animation software
How to use "PENCIL" animation software
 

Similar to Country State City Dropdown in PHP

Intro to php
Intro to phpIntro to php
Intro to php
Sp Singh
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applicationselliando dias
 
My sql regis_handsonlab
My sql regis_handsonlabMy sql regis_handsonlab
My sql regis_handsonlab
sqlhjalp
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
Dave Stokes
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
Jalpesh Vasa
 
Connecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptxConnecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptx
TempMail233488
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Ayes Chinmay
 
Database presentation
Database presentationDatabase presentation
Database presentationwebhostingguy
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
Rasan Samarasinghe
 
PHP webboard
PHP webboardPHP webboard
PHP webboard
tumetr1
 
MySQL with PHP
MySQL with PHPMySQL with PHP
MySQL with PHP
MsSJeyalakshmiVelsUn
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Alex Sharp
 
Database Implementation Final Document
Database Implementation Final DocumentDatabase Implementation Final Document
Database Implementation Final DocumentConor O'Callaghan
 
Practical MySQL.pptx
Practical MySQL.pptxPractical MySQL.pptx
Practical MySQL.pptx
HussainUsman4
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2ADARSH BHATT
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
Hugo Hamon
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programmingCasear Chu
 

Similar to Country State City Dropdown in PHP (20)

Mysql & Php
Mysql & PhpMysql & Php
Mysql & Php
 
Intro to php
Intro to phpIntro to php
Intro to php
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
My sql regis_handsonlab
My sql regis_handsonlabMy sql regis_handsonlab
My sql regis_handsonlab
 
All Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for NewbiesAll Things Open 2016 -- Database Programming for Newbies
All Things Open 2016 -- Database Programming for Newbies
 
Intoduction to php restful web service
Intoduction to php  restful web serviceIntoduction to php  restful web service
Intoduction to php restful web service
 
4.3 MySQL + PHP
4.3 MySQL + PHP4.3 MySQL + PHP
4.3 MySQL + PHP
 
Connecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptxConnecting_to_Database(MySQL)_in_PHP.pptx
Connecting_to_Database(MySQL)_in_PHP.pptx
 
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
Internet and Web Technology (CLASS-10) [Node.js] | NIC/NIELIT Web Technology
 
Database presentation
Database presentationDatabase presentation
Database presentation
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
PHP webboard
PHP webboardPHP webboard
PHP webboard
 
MySQL with PHP
MySQL with PHPMySQL with PHP
MySQL with PHP
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
Database Implementation Final Document
Database Implementation Final DocumentDatabase Implementation Final Document
Database Implementation Final Document
 
Database api
Database apiDatabase api
Database api
 
Practical MySQL.pptx
Practical MySQL.pptxPractical MySQL.pptx
Practical MySQL.pptx
 
Learn PHP Lacture2
Learn PHP Lacture2Learn PHP Lacture2
Learn PHP Lacture2
 
The History of PHPersistence
The History of PHPersistenceThe History of PHPersistence
The History of PHPersistence
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programming
 

More from Vineet Kumar Saini

Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
Vineet Kumar Saini
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
Vineet Kumar Saini
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
Vineet Kumar Saini
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
Vineet Kumar Saini
 
Programming in C
Programming in CProgramming in C
Programming in C
Vineet Kumar Saini
 

More from Vineet Kumar Saini (20)

Introduction to Html
Introduction to HtmlIntroduction to Html
Introduction to Html
 
Stripe in php
Stripe in phpStripe in php
Stripe in php
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
Install Drupal on Wamp Server
Install Drupal on Wamp ServerInstall Drupal on Wamp Server
Install Drupal on Wamp Server
 
Joomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDFJoomla 2.5 Tutorial For Beginner PDF
Joomla 2.5 Tutorial For Beginner PDF
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Update statement in PHP
Update statement in PHPUpdate statement in PHP
Update statement in PHP
 
Delete statement in PHP
Delete statement in PHPDelete statement in PHP
Delete statement in PHP
 
Types of Error in PHP
Types of Error in PHPTypes of Error in PHP
Types of Error in PHP
 
GET and POST in PHP
GET and POST in PHPGET and POST in PHP
GET and POST in PHP
 
Database connectivity in PHP
Database connectivity in PHPDatabase connectivity in PHP
Database connectivity in PHP
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
Programming in C
Programming in CProgramming in C
Programming in C
 
Browser information in PHP
Browser information in PHPBrowser information in PHP
Browser information in PHP
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Variables in PHP
Variables in PHPVariables in PHP
Variables in PHP
 
MVC in PHP
MVC in PHPMVC in PHP
MVC in PHP
 
SQL Limit in PHP
SQL Limit in PHPSQL Limit in PHP
SQL Limit in PHP
 
Print function in PHP
Print function in PHPPrint function in PHP
Print function in PHP
 

Recently uploaded

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
Celine George
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
Jisc
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
DeeptiGupta154
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
GeoBlogs
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 

Recently uploaded (20)

Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
How to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERPHow to Create Map Views in the Odoo 17 ERP
How to Create Map Views in the Odoo 17 ERP
 
How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...How libraries can support authors with open access requirements for UKRI fund...
How libraries can support authors with open access requirements for UKRI fund...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Overview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with MechanismOverview on Edible Vaccine: Pros & Cons with Mechanism
Overview on Edible Vaccine: Pros & Cons with Mechanism
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
The geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideasThe geography of Taylor Swift - some ideas
The geography of Taylor Swift - some ideas
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 

Country State City Dropdown in PHP

  • 1. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com Country State City in Dropdown list in PHP using Ajax First of all we create a database then we will create three tables in databse i.e. country , state and city table. Like as following images. Country Table CREATE TABLE `country` ( `Country_Id` varchar(255) NOT NULL, `CountryName` varchar(255) NOT NULL ); INSERT INTO `country` (`Country_Id`, `CountryName`) VALUES ('1', 'India'), ('2', 'Usa'), ('3', 'Canada'); State Table CREATE TABLE `state` ( `StateId` varchar(255) NOT NULL, `Country_Id` int(11) NOT NULL, `StateName` varchar(255) NOT NULL ); INSERT INTO `state` (`StateId`, `Country_Id`, `StateName`) VALUES ('1', 1, 'Orissa'), ('2', 1, 'Andhra Pradesh'), ('3', 1, 'West Bengal'), ('4', 1, 'Maharastra'), ('5', 2, 'Arizona'), ('6', 2, 'California'), ('7', 2, 'Texas'), ('8', 3, 'Alberta'), ('9', 3, 'Ontario');
  • 2. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com City Table CREATE TABLE `city` ( `CityId` varchar(255) NOT NULL, `State_Id` int(11) NOT NULL, `CityName` varchar(255) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; INSERT INTO `city` (`CityId`, `State_Id`, `CityName`) VALUES ('1', 1, 'Bhubaneswar'), ('2', 2, 'Hyderbad'), ('3', 3, 'Kolkata'), ('4', 4, 'Mumbai'), ('5', 5, 'Calgary'), ('6', 5, 'Edmonton'), ('7', 5, 'Duffield'), ('8', 6, 'Phoenix'), ('9', 7, 'San Diego'), ('10', 8, 'Austin'), ('11', 9, 'Toronto');
  • 3. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com Now we will create configuration file in php i.e. config.php. I which file we will create the database connectivity. config.php file <?php $host="localhost"; $username="root"; $password=""; $dbname="country_dropdown"; $con=mysql_connect("$host","$username","$password"); mysql_select_db("$dbname",$con); ?> Now we will create index.php Index.php <?php include('config.php'); $Country_query="select id,country_name from country order by country_name asc "; $Country=mysql_query($Country_query); $Count_Country=mysql_num_rows($Country); ?> <html> <head> <title>Country State City Drop Down Using Ajax in PHP</title> <!--<script type="text/javascript" src="js/jquery-1.4.1.min.js"></script>--> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"> <script type="text/javascript"> function selectCity(country_id){ if(country_id!="-1"){ loadData('state',country_id); $("#city_dropdown").html("<option value='-1'>Select city</option>"); }else{ $("#state_dropdown").html("<option value='-1'>Select state</option>"); $("#city_dropdown").html("<option value='-1'>Select city</option>"); } } function selectState(state_id){ if(state_id!="-1"){ loadData('city',state_id); }else{ $("#city_dropdown").html("<option value='-1'>Select city</option>") } } function loadData(loadType,loadId){ var dataString = 'loadType='+ loadType +'&loadId='+ loadId;
  • 4. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com $.ajax({ type: "POST", url: " load_state_city.php", data: dataString, cache: false, success: function(result){ $("#"+loadType+"_loader").hide(); $("#"+loadType+"_dropdown").html("<option value='-1'>Select +loadType+"</option>"); $("#"+loadType+"_dropdown").append(result); } }); } </script> </head> <body> <?php if($Count_Country > 0){ ?> <table> <tr> <td>Country:</td> <td> <select onchange="selectCity(this.options[this.selectedIndex].value)"> <option value="-1">Select country</option> <?php while($rowCountry=mysql_fetch_array($Country)){ ?> <option value="<?php echo $rowCountry['id']?>"><?php echo $rowCountry['country_name']?></option> <?php } ?> </select> </td> </tr> <tr> <td>State:</td> <td> <select id="state_dropdown" onchange="selectState(this.options[this.selectedIndex].value)"> <option value="-1">Select state</option> </select><span id="state_loader"></span> </td> </tr> <tr> <td>City:</td> <td>
  • 5. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com <select id="city_dropdown"> <option value="-1">Select city</option> </select><span id="city_loader"></span> </td> </tr> </table> <?php }else{ echo 'No Country Name Found'; } ?> </body> </html> load_state_city.php <?php include('config.php'); $loadType=$_POST['loadType']; $loadId=$_POST['loadId']; if($loadType=="state"){ $sql="select id,state_name from state where country_id='".$loadId."' order by state_name asc"; }else{ $sql="select id,city_name from city where state_id='".$loadId."' order by city_name asc"; } $res=mysql_query($sql); $check=mysql_num_rows($res); if($check > 0){ $HTML=""; while($row=mysql_fetch_array($res)){ $HTML.="<option value='".$row['id']."'>".$row['1']."</option>"; } echo $HTML; } ?> Output When we run the index.php file then we will get following output. Suppose we select Canada from country dropdown then display state in state dropdown along with Canada. When we select state from state dropdown then city will display in city dropdown along with selected state.
  • 6. PHP Tutorials By vineet Kumar Saini www.vineetsaini.wordpress.com .1 .2 .3 .4 Conclusion Using this article one can get easily understand cascading dropdown list in php.