<html>
<head>
</head>

<body>

<script type="text/javascript">
function ajaxFunction(choice)
{

var httpxml;
try
  {
  // Firefox, Opera 8.0+, Safari
  httpxml=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    httpxml=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      httpxml=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
function stateChanged() 
    {
    if(httpxml.readyState==4)
      {
//alert(httpxml.responseText);
var myObject = JSON.parse(httpxml.responseText);

for(j=document.myForm.state.options.length-1;j>=0;j--)
{
document.myForm.state.remove(j);
}

var state1=myObject.value.state;
for (i=0;i<myObject.state.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myObject.state[i];
optn.value = myObject.state[i];
document.myForm.state.options.add(optn);
if(optn.value==state1){
document.myForm.state.options[i].selected=true;
}
} 

///////Remove options from  list box ///////////
for(j=document.myForm.city.options.length-1;j>=0;j--)
{
document.myForm.city.remove(j);
}

var city1=myObject.value.city;
/// Add options to list box ///
for (i=0;i<myObject.city.length;i++)
{
var optn = document.createElement("OPTION");
optn.text = myObject.city[i];
optn.value = myObject.city[i];
document.myForm.city.options.add(optn);
if(optn.value==city1){
document.myForm.city.options[i].selected=true;
}

} 


///////////////////////////
    }
    }

var url="ajax-dd3ck.php";
var FINALPRODUCTCODE=myForm.FINALPRODUCTCODE.value;
if(choice != 's1'){
var state=myForm.state.value;
var city=myForm.city.value;
}else{
var state='';
var city='';
}
url=url+"?FINALPRODUCTCODE="+FINALPRODUCTCODE;
url=url+"&state="+state;
url=url+"&city="+city;
url=url+"&id="+Math.random();
myForm.st.value=state;
//alert(url);
httpxml.onreadystatechange=stateChanged;
httpxml.open("GET",url,true);
httpxml.send(null);
}
</script>


</head>

<body >
</head>

<body>
<form name="myForm" action='#' method='post'">
<input type=hidden name=st value=0>
<table width=500>
<tr><td >
Select Country<br><select name=FINALPRODUCTCODE id='s1' onchange=ajaxFunction('s1');>
<option value=''>Select One</option>
<?Php
$dbhost_name = "localhost"; // Your host name 
$database = "ams";       // Your database name
$username = "root";            // Your login userid 
$password = "";            // Your password 
//////// End of database details of your server //////

//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host='.$dbhost_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
$sql="select distinct FINALPRODUCTCODE from student5 ";
foreach ($dbo->query($sql) as $row) {
echo "<option value=$row[FINALPRODUCTCODE]>$row[FINALPRODUCTCODE]</option>";
}
?>
</select>

</td><td ><select name=state  onchange=ajaxFunction('s2');>
<option value=''>Select One</option></select>
</td>
<td ><select name=city onchange=ajaxFunction('s3');>
<option value=''>Select One</option></select></td>
</tr></tr>
<tr><td colspan=3><input type="submit" value='Submit'></td></tr>
</form>
</table>
<br><br>
</html>





<?Php
$dbhost_name = "localhost"; // Your host name 
$database = "ams";       // Your database name
$username = "root";            // Your login userid 
$password = "";            // Your password 
//////// End of database details of your server //////

//////// Do not Edit below /////////
try {
$dbo = new PDO('mysql:host='.$dbhost_name.';dbname='.$database, $username, $password);
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}error_reporting(0);// With this no error reporting will be there
//////////
/////////////////////////////////////////////////////////////////////////////
$fp=$_GET['FINALPRODUCTCODE'];// 
//$country='IND'; // To check you can use this
$ps=$_GET['state'];
$d=$_GET['city'];
//$state1="Andhra Pradesh";
///////////// Validate the inputs ////////////
// Checking country variable ///
if((strlen($fp)) > 0 and (!ctype_alpha($fp))){ 
echo "Data Error";
exit;
}
if ((strlen($ps)) > 0 and ctype_alpha(str_replace(' ', '', $ps)) === false) {
echo "Data Error";
exit;
}


if(strlen($fp) > 0){
$q_co="select distinct(state) from student5 where FINALPRODUCTCODE = '$fp'";
}else{
$q_co="select distinct(state) from student5";
}
$sth = $dbo->prepare($q_co);
$sth->execute();
$state = $sth->fetchAll(PDO::FETCH_COLUMN);

$q_st="select distinct(city) from student5 where ";
if(strlen($fp) > 0){
$q_st= $q_st . " FINALPRODUCTCODE = '$fp' ";
}
if(strlen($ps) > 0){$q_st= $q_st . " and  state='$ps'";}
$sth = $dbo->prepare($q_st);
$sth->execute();
$city = $sth->fetchAll(PDO::FETCH_COLUMN);

$main = array('state'=>$state,'city'=>$city,'value'=>array("state"=>"$ps","city"=>"$city"));
echo json_encode($main);
?>
Member Avatar for diafol

So what is this? A code snippet or a question asking for help? Either way, we need some sort of idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.