Use the database file ( http://pastebin.com/raw/2x4sFMTd ) to design an HTML for
ID: 3840150 • Letter: U
Question
Use the database file ( http://pastebin.com/raw/2x4sFMTd ) to design an HTML form to enter a country and the php code loads information about the country and a list of its cities.
Use the database file ( http://pastebin.com/raw/2x4sFMTd ) to design an HTML form to enter a country and the php code loads information about the country and a list of its cities.
Use the database file ( http://pastebin.com/raw/2x4sFMTd ) to design an HTML form to enter a country and the php code loads information about the country and a list of its cities.
Explanation / Answer
Tables Creations:
CREATE TABLE country (id tinyint(4) NOT NULL auto_increment, country varchar(20) NOT NULL default '', PRIMARY KEY (`id`))
CREATE TABLE state (id tinyint(4) NOT NULL auto_increment, countryid tinyint(4) NOT NULL, statename varchar(40) NOT NULL, PRIMARY KEY (`id`))
CREATE TABLE city (id tinyint(4) NOT NULL auto_increment, city varchar(50) default NULL, stateid tinyint(4) default NULL, countryid tinyint(4) NOT NULL, PRIMARY KEY (`id`))
Index.php:
<form method="post" action="" name="form1">
<center>
<table width="45%" cellspacing="0" cellpadding="0">
<tr>
<td width="75">Country</td>
<td width="50">:</td>
<td width="150">
<select name="country">
<option value="">Select Country</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['country']?> </option>
<?php } ?>
</select>
</td>
</tr>
<tr >
<td>State</td>
<td width="50">:</td>
<td >
<div id="statediv">
<select name="state" >
<option>Select State</option>
</select>
</div>
</td>
</tr>
<tr>
<td>City</td>
<td width="50">:</td>
<td>
<div id="citydiv">
<select name="city">
<option>Select City</option>
</select>
</div>
</td>
</tr>
</table>
</center>
</form>
getstate() Function:
function getState(countryId) {
var strURL="findState.php?country="+countryId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('statediv').innerHTML=req.responseText;
document.getElementById('citydiv').innerHTML='<select name="city">'+
'<option>Select City</option>'+'</select>';
} else {
alert("Problem while using XMLHTTP: " + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
findState.php:
<?php
$country = intval($_GET['country']);
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test');
$query = "SELECT id,statename FROM state WHERE countryid='$country'";
$result = mysql_query($query);
?>
<select name="state">
<option>Select State</option>
<?php while ($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>
<?php } ?>
</select>
getCity() Function:
function getCity(countryId,stateId) {
var strURL="findCity.php?country="+countryId+"&state="+stateId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById('citydiv').innerHTML=req.responseText;
} else {
alert("Problem while using XMLHTTP: " + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
findcity.php:
<?php
$countryId = intval($_GET['country']);
$stateId = intval($_GET['state']);
$con = mysql_connect('localhost', 'root', '');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('test');
$query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
$result=mysql_query($query);
?>
<select name="city">
<option>Select City</option>
<?php while($row=mysql_fetch_array($result)) { ?>
<option value=<?php echo $row['id']?>><?php echo $row['city']?></option>
<?php } ?>
</select>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.