To add state information according to the selected country using PHP and MySQL, here's the process in simple terms:
Database Setup:
Create two tables: one for countries and one for states.
The countries table holds the list of countries.
The states table stores the states along with a reference (foreign key) to the country they belong to.
Insert Data:
Add some sample countries like "USA", "India", and "Canada" in the countries table.
Add related states like "California" and "New York" for the USA, and "Maharashtra" and "Gujarat" for India in the states table, with a link to the respective country.
User Interface:
Display a country dropdown in an HTML form.
When the user selects a country from this dropdown, an AJAX request is sent to the server to retrieve the corresponding states for that country.
Server-Side Processing:
On the server-side (PHP), receive the selected country ID from the AJAX request.
Query the states table in the database to fetch the states related to the chosen country.
Send back the list of states as options to be displayed in a second dropdown (the states dropdown).
User Experience:
The user first selects a country, and the states dropdown automatically updates with the relevant states.
This is done without needing to refresh the page, creating a smooth and dynamic experience.
In summary, the process involves linking states to countries in the database, dynamically fetching states based on the selected country using PHP, and updating the UI with the state data via AJAX.
Here is one example of it
<html>
<head>
<title>Title of Page</title>
<script language="javascript" type="text/javascript">
function showState(countryid)
{
document.form1.submit();
}
</script>
<?php
include('myconnection.php');
if(isset($_POST['submit']))
{
$sname=trim($_POST['statename']);
$cid=trim($_POST['countryid']);
if(empty($sname))
{
echo "<center><p style=\"color:Red\" >Please Enter State name</p><center>";
}
else
{ $sql="insert into tblstate(`id`,`countryid`,`statename`) values(NULL,'$cid','$sname');";
if(mysqli_query($con,$sql))
{
echo "<center><p style=\"background-color:#000;width:450px;color:#fdd000\" >Statename is successfully inserted !!!</p><center>";
}
else
{
echo "error".mysqli_error();
}
}
}
?>
</head>
<body>
<form id="form1" name="form1" method="POST" action="">
<center><fieldset style="width:450px">
<legend ><h1 style="background-color:#000fdd;color:#ffff00">Add New State</h1></legend>
<table>
<tr><td colspan="2"></td></tr>
<tr>
<td>Select Country</td>
<td>
<?php
$query = "SELECT * FROM tblcountry";
$result = mysqli_query($con,$query);
?>
<select name="countryid" id="countryid" onChange="showState(this.value);">
<option value="">--- Select Country---</option>
<?php
while ($line = mysqli_fetch_array($result)) {
?>
<option value="<?php echo $line['id'];?>" > <?php echo $line['countryname'];?> </option>
<?php
}
?>
</td></tr>
<tr><td>StateName</td><td><input type="text" name="statename" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="submit" value="submit"><input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</fieldset>
</center>
</form>
</body>
</html>
website :http://www.vataliyacomputer.in
Contact No: 9726185104,8780972204