0% found this document useful (0 votes)
7 views4 pages

Practical-9 21012011092

The document outlines a practical assignment for creating a Country, State, and City dropdown using Ajax in PHP and MySQL. It includes code snippets for the main HTML file (Ajax.php) and three PHP files (get_countries.php, get_states.php, get_cities.php) that handle database connections and data retrieval. The aim is to dynamically populate the dropdowns based on user selections, enhancing user experience in web applications.

Uploaded by

Op Megh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Practical-9 21012011092

The document outlines a practical assignment for creating a Country, State, and City dropdown using Ajax in PHP and MySQL. It includes code snippets for the main HTML file (Ajax.php) and three PHP files (get_countries.php, get_states.php, get_cities.php) that handle database connections and data retrieval. The aim is to dynamically populate the dropdowns based on user selections, enhancing user experience in web applications.

Uploaded by

Op Megh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

GANPAT UNIVERSITY

U.V. PATEL COLLEGE OF ENGINEERING B.Tech


6th Semester CE/IT

2CEIT6PE1: Web Technology

Practical-9

Aim: Create Country State City Dropdown using Ajax in PHP MySQL.

Code:

Ajax.php:

<!DOCTYPE html>
<html>
<head>
<title>Country State City Dropdown</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.ajax({
url: "get_countries.php",
type: "POST",
success: function(data){
$("#country").html(data);
}
});
$("#country").change(function(){
var country_id = $(this).val();
$.ajax({
url: "get_states.php",
type: "POST",
data: {country_id: country_id},

21012011092_megh patel CE-E2


success: function(data){
$("#state").html(data);
}
});
});

$("#state").change(function(){
var state_id = $(this).val();
$.ajax({
url: "get_cities.php",
type: "POST",
data: {state_id: state_id},
success: function(data){
$("#city").html(data);
}
});
});
});
</script>
</head>
<body>
<h2>Country, State, City Dropdown</h2>
<div>
<label>Country:</label>
<select id="country">
<option value="">Select Country</option>
</select>
</div>
<div>
<label>State:</label>
<select id="state">
<option value="">Select State</option>
</select>
</div>
<div>
<label>City:</label>
<select id="city">
<option value="">Select City</option>
</select>
</div>
</body>
</html>

21012011092_megh patel CE-E2


get_countries.php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "php";

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name FROM countries";


$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
echo "<option value='".$row['id']."'>".$row['name']."</option>";
}

$conn->close();
?>

get_states.php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "php";

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$country_id = $_POST['country_id'];
$sql = "SELECT id, name FROM states WHERE country_id = $country_id";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {

21012011092_megh patel CE-E2


echo "<option value='".$row['id']."'>".$row['name']."</option>";
}

$conn->close();
?>

get_cities.php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "php";

$conn = new mysqli($host, $user, $pass, $db);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$state_id = $_POST['state_id'];
$sql = "SELECT id, name FROM cities WHERE state_id = $state_id";
$result = $conn->query($sql);

while($row = $result->fetch_assoc()) {
echo "<option value='".$row['id']."'>".$row['name']."</option>";
}

$conn->close();
?>

Output:

21012011092_megh patel CE-E2

You might also like