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>

Recommended Answers

All 9 Replies

In your while loop, for the item that matches $q , add the following attribute: selected="selected"

commented: Useful Post +7

Replace this

<option value="<?php echo $row['sid'] ?>"><?php echo $row['name'] ?></option>

with

<option value="<?php echo $row['sid'] ?>" <?php if($_POST["u_state"]==$row['sid']) echo "selected"; ?>><?php echo $row['name'] ?></option>

for city

<option  value="<?php echo $row['cityid'] ?>" <?php if($_POST["s_city"]==$row['cityid']) echo "selected"; ?>><?php echo $row['cityname'] ?></option>

Thanks..

I am not getting value of city.when i am trying to echo $_POST in other page(not in getcity.php)
after refresh city dropdown is not showing.I tried if else but not working.
suggest me ??

Replace this

<option value="<?php echo $row['sid'] ?>"><?php echo $row['name'] ?></option>

with

<option value="<?php echo $row['sid'] ?>" <?php if($_POST["u_state"]==$row['sid']) echo "selected"; ?>><?php echo $row['name'] ?></option>

for city

<option  value="<?php echo $row['cityid'] ?>" <?php if($_POST["s_city"]==$row['cityid']) echo "selected"; ?>><?php echo $row['cityname'] ?></option>

You want it so the select box remembers what city you have picked?

var lastTown = '<?php if(ctype_alnum($_GET['town'])){echo $_GET['town'];}?>';
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){
			lastTown = str;
			document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
		}
	}
	xmlhttp.open("GET","getcity.php?q="+str,true);
	xmlhttp.send();
}


/*put this at end of page*/
if(lastTown != ''){
	showUser(lastTown);
}

The Problem is javascript is client-side so on page refresh anything picked in javascript is lost, you could get the page to redirect to itself with a GET variable of the town - then the user can refresh that as much as he wants and get the same town.

eg.

window.location = 'mypage.php?town='+str;

Thanks..

I am not getting value of city.when i am trying to echo $_POST in other page(not in getcity.php)
after refresh city dropdown is not showing.I tried if else but not working.
suggest me ??

post vars are sent from one page such as a form to a formprocess page. Just requesting a URL does not send any post vars so there is nothing to print out - it hasn't got any post vars.

You should set a default city incase it is empty

In which situation the page get refreshed?

In which situation the page get refreshed?

hi,
Suppose i select new delhi city,now it is showing all the towns of new delhi.
when i got some validation error,i am trying to remain the selected town of selected city.
n i am not getting the value of selected town if there is no error.
Need Suggestion..

At end of your page place this code

<?php
if(isset($_POST["u_state"]))
{
?>
<script type="text/javascript">
showUser(<?php echo $_POST["u_state"]; ?>);
</script>
<?php
}
?>

Thanks Sir,
My main problem is am not getting value of city in ab.php
When i echo city value in getcity.php it is showing.
Please Suggest.

ab.php

<tr>
<td style="padding-top:8px;">State:</td>
 <td>
<table>
 <tr>
  <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))
  {
?>
					          
<option value="<?php echo $row['sid'] ?>" <?php if($_POST["u_state"]==$row['sid']) echo "selected"; ?>><?php echo $row['name'] ?></option>
 <?php
}
 ?>
</select>
 </td>
<td><div id="txtHint">
					           
<select style="width:120px" name="u_city">
<option>--Select City--</option>
 </select>
 </div>
</td>
</tr>

getcity.php

include('database_ihm.php');
$q=$_GET["q"];
$sql="SELECT * FROM city WHERE sid = '".$q."'";
//$sql="SELECT * FROM city WHERE sid = '4'";
$query=mysql_query($sql);
?>
<select name="u_city" style="width:150px">
<?php
while($row=mysql_fetch_array($query))
{
?>
<option value="<?php echo $row['cityid'] ?>" <?php if($_POST["s_city"]==$row['cityid']) echo "selected"; ?>><?php echo $row['cityname'] ?></option>
<?php
}
?>
</select>
<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();
}
</script>

At end of your page place this code

<?php
if(isset($_POST["u_state"]))
{
?>
<script type="text/javascript">
showUser(<?php echo $_POST["u_state"]; ?>);
</script>
<?php
}
?>
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.