Today I am going to explain you how we can add rows in Select Tag Using PHP code. You have to read the example below.
Step1: Create Database
Create database test_db;
Create Table location;
Create table location(country char(50),state char(50));
Insert some data into the table location for testing purpose
insert into location values('India','Uttar Pardesh');
insert into location values('India','New Delhi');
insert into location values('India','MP');
insert into location values('Saudi Arabia','Jeddah');
insert into location values('Saudi Arabia','Riyad');
insert into location values('Saudi Arabia','Jizan');
Step2: create a file index.php
<html>
<head>
<script>
function showState(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("state").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getState.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<?php
$con = mysqli_connect('localhost','root','root','test_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT distinct country FROM location";
$result = mysqli_query($con,$sql);
echo "<select name='country' onchange='showState(this.value)'>";
echo "<option value=''>Select Country:</option>";
while($row = mysqli_fetch_array($result)){
echo "<option value='".$row['country']."'>".$row['country']."</option>";
}
echo "</select>";
?>
<select name="state" id="state">
<option value="">Select State:</option>
</select>
</body>
</html>
<head>
<script>
function showState(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("state").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET","getState.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<?php
$con = mysqli_connect('localhost','root','root','test_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
$sql = "SELECT distinct country FROM location";
$result = mysqli_query($con,$sql);
echo "<select name='country' onchange='showState(this.value)'>";
echo "<option value=''>Select Country:</option>";
while($row = mysqli_fetch_array($result)){
echo "<option value='".$row['country']."'>".$row['country']."</option>";
}
echo "</select>";
?>
<select name="state" id="state">
<option value="">Select State:</option>
</select>
</body>
</html>
Related Post:
Step3: create a file getState.php
<?php
//for integer value
//$q = intval($_GET['q']);
//for string value
$q = ($_GET['q']);
echo $q;
$con = mysqli_connect('localhost','root','root','test_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT state FROM location WHERE country = '".$q."'";
$result = mysqli_query($con,$sql);
if($result>0){
echo "<option value=''>Select State:</option>";
while($row = mysqli_fetch_array($result)){
echo "<option value='".$row['state']."'>".$row['state']."</option>";
}
}
mysqli_close($con);
?>
Output:
Note: If you like this code or having any question then reply me in comment i will help you.
3 Comments
Nice code, good job
ReplyDeleteNice :) thanks :D
ReplyDeleteThe PHP programmers can easily enhance the web application's performance by not executing database queries in loop.plakatų spausdinimas
ReplyDelete