SlideShare uma empresa Scribd logo
1 de 6
Baixar para ler offline
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.

Mais conteúdo relacionado

Mais procurados

SQL Queries
SQL QueriesSQL Queries
SQL QueriesNilt1234
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptWalid Ashraf
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptWojciech Dzikowski
 
Entity Framework Core
Entity Framework CoreEntity Framework Core
Entity Framework CoreKiran Shahi
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKJosé Paumard
 
JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cstewashton
 
React js programming concept
React js programming conceptReact js programming concept
React js programming conceptTariqul islam
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to phpAnjan Banda
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...Edureka!
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework Serhat Can
 

Mais procurados (20)

SQL Queries
SQL QueriesSQL Queries
SQL Queries
 
React JS - Introduction
React JS - IntroductionReact JS - Introduction
React JS - Introduction
 
Javascript essentials
Javascript essentialsJavascript essentials
Javascript essentials
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Sql commands
Sql commandsSql commands
Sql commands
 
Lab #2: Introduction to Javascript
Lab #2: Introduction to JavascriptLab #2: Introduction to Javascript
Lab #2: Introduction to Javascript
 
React js
React jsReact js
React js
 
ES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern JavascriptES2015 / ES6: Basics of modern Javascript
ES2015 / ES6: Basics of modern Javascript
 
Entity Framework Core
Entity Framework CoreEntity Framework Core
Entity Framework Core
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
JSON in Oracle 18c and 19c
JSON in Oracle 18c and 19cJSON in Oracle 18c and 19c
JSON in Oracle 18c and 19c
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
React js programming concept
React js programming conceptReact js programming concept
React js programming concept
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
MERN PPT
MERN PPTMERN PPT
MERN PPT
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Intro to React
Intro to ReactIntro to React
Intro to React
 
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
JavaScript Tutorial For Beginners | JavaScript Training | JavaScript Programm...
 
Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 

Destaque

Destaque (8)

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
 
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
 
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
 

Semelhante a Country State City Dropdown in PHP

Intro to php
Intro to phpIntro to php
Intro to phpSp 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_handsonlabsqlhjalp
 
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 NewbiesDave Stokes
 
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.pptxTempMail233488
 
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 DatabasesRasan Samarasinghe
 
PHP webboard
PHP webboardPHP webboard
PHP webboardtumetr1
 
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.pptxHussainUsman4
 
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 PHPersistenceHugo Hamon
 
Hd insight programming
Hd insight programmingHd insight programming
Hd insight programmingCasear Chu
 

Semelhante a 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
 

Mais de 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
 

Último

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Celine George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 

Último (20)

Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17Difference Between Search & Browse Methods in Odoo 17
Difference Between Search & Browse Methods in Odoo 17
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 

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.