I want to retain selected value of city drop down list after refresh(any error occour).
Need help
Javascript code
<script type="text/javascript">
function showUser(str)
{
if (str=="")
{
document.getElementById("txtHint").innerHTML="";
return;
}
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("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getcity.php?q="+str,true);
xmlhttp.send();
}
<td>
<select name="u_state" style="width:150px" onchange="showUser(this.value)">
<option value="0">--Select state--</option>
<?php
$result=mysql_query("SELECT * from state");
while($row=mysql_fetch_array($result))
{
//echo $row['sid'];
?>
<option value="<?php echo $row['sid'] ?>"><?php echo $row['name'] ?></option>
<?php
}
?>
</select>
</td>
<td><div id="txtHint"></div></td>
getcity.php
<?php
include('database.php');
$q=$_GET["q"];
$sql="SELECT * FROM city WHERE sid = '".$q."'";
$query=mysql_query($sql);
?>
<select name="s_city" style="width:150px">
<?php
while($row=mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['cityid'] ?>"><?php echo $row['cityname'] ?></option>
<?php
}
?>
</select>