I want to keep what the user selected so when he clicks back and goes to my search the values are still there, have three listboxes. Country, State, City


Country Listbox

<select name="country" id="country" onChange="filter_state();">              
							
<?php
		
echo "<option selected>" . $_POST['country']; "</option>";
		
$sql = "SELECT * FROM tbl_country";
$result = mysql_query($sql);
$i=0;
while($row = mysql_fetch_assoc($result)){
if($i <= 0){
$country = $row["id"]; 
}
echo "<option value='".$row['id']."'>".$row['fld_country']."</option>";
$i++;
}
?>
</select>

State Listbox

<select name="state" id="state" onChange="filter_city();">
<?php
							  
echo "<option selected>" . $_GET['state']; "</option>";
						
$sql = "SELECT * FROM tbl_state where fld_country=$country";
$result = mysql_query($sql);		
$j=0;
while($row = mysql_fetch_assoc($result)){
if($j <= 0){
$state = $row["id"];
}
echo "<option value='".$row['id']."'>".$row['fld_state']."</option>";
$j++;
}
?>
</select>

City listbox

<select name="city" id="city">
<?php
//echo "<option selected>" . $_GET['city']"</option>";
						
$sql = "SELECT * FROM tbl_city where fld_countryid=$country AND fld_stateid=$state";
$result = mysql_query($sql);		
while($row = mysql_fetch_assoc($result)){
echo "<option value='".$row['id']."'>".$row['fld_city']."</option>";
$i++;
}
?>
</select>

The line

echo "<option selected>" . $_GET['state']; "</option>";

is what i want to try use, but it leaves it empty each time

I think its better to use session variables to hold the values of selected option after it is posted and in this page inside while loop where the options are added to listbox, you can have a check there to have the option as selected which was selected by the user...

Yes adkhan is write use session variables to hold the values you last selected and then when you click back....accordingly mark selected one....
For using session you must keep it in mind that don't forget to start session using

<?php
session_start();
?>

and also it must be the first statement.....

Thanx Adkhan and IIM you were rite, i was scared to use session never have before, this is what i ended up doing


In my results page, reason for this is that user had to have selected data to get here so then i can get the values from form

//RESULTS PAGE

session_start();

//STORE SESSION DATA

$_SESSION['countryid']= $_GET['country'];
$_SESSION['stateid']= $_GET['state'];
$_SESSION['cityid']= $_GET['city'];

this populates the session with all the goodies i need :)

<select name="city" id="city">

<?php

//SEE IF THERE IS SESSION DATA FOR CITY

if ( $_SESSION["stateid"] == "" ) 
		{
		
		//JUST FETCH THE DATA AND PUT IN LISTBOX
		$sql = "SELECT * FROM tbl_city where fld_countryid=$country AND fld_stateid=$state";
		
		$result = mysql_query($sql);		
		while($row = mysql_fetch_assoc($result)){
		echo "<option value='".$row['id']."'>".$row['fld_city']."</option>";
		$i++;
		}

} else {

	$countryId = $_SESSION["countryid"];
	$stateId = $_SESSION["stateid"];
		
	//DISPLAY WHAT WAS SELECTED BY THE USER
	echo "<option selected value =" .$_SESSION['cityid'].">".$_SESSION['cityname']."</option>";
		
	//GET DATA RELATED TO THE PREVIOUS SATE SELECTION TO FILL THE LISTBOX AGAIN FOR NEXT SELECTION
	$sql = "SELECT * FROM tbl_city where fld_countryid=$countryId AND fld_stateid=$stateId";
	$result = mysql_query($sql);
		
	while($row = mysql_fetch_assoc($result)){
	echo "<option value='".$row['id']."'>".$row['fld_city']."</option>";
	}
}

?>
</select>

The end result was it works now, I never knew Session were that easy.
Thank you again for pointing me in the direction, i'm new at this so please don't laugh at me code :)

mark the thread as read by clicking on the link provided below this thread ......
If your problem is solved...

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.