Link to home
Start Free TrialLog in
Avatar of rpkhare
rpkhareFlag for India

asked on

Retain DropDown last selected text after POST.

I want to maintain the last selected text of DropDown when Form returns from POST. I am filling DropDown dynamically and tried using following code, but it is not retaining last text. It is successfully filling the DropDown.
<?php
	require("dbconnection.php");
	require("dbaccess.php");

	$dropdownControlName = $_GET['DropDownControlName'];
	$query = $_GET['SqlQuery'];
	dbconnection::OpenConnection();
	$result = dbaccess::GetRows($query);
?>
<select id="<?php echo $dropdownControlName; ?>" name="<?php echo $dropdownControlName; ?>">
<option>Select from the list</option>
<?php while($row=mysql_fetch_array($result))
{ ?>
	<option selected="<?php echo $row[1]; ?>" value="<?php echo $row[0]; ?>"><?php echo $row[1]; ?></option>
<?php } ?>
</select>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kalpan
Kalpan
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ludofulop
ludofulop

mostly it is done this way:

<form method="post">
<select name="selname">
<?php
  $options = getOptionsFromDatabase();
  foreach ($options as $option)
  {
     echo '<option '.($_POST['selname']==$options['value']?'selected="selected"':'').' value="'.$options['value'].'">'.$options['name'].'</option>';
  }
?>
</select>
...
</form>
Avatar of rpkhare

ASKER

@kalmax:

It can be handled with a cookie also? Which one is more faster, session or cookie?
it depends if you maintaining the data with session than session would be more appropriate...while if you need this for offline setting when user comes online he/she could retain the same value....session can only be usefull for online...cookie is more faster coz this would be set on the clients browser...

setcookie("lastSelected", $value);

but be careful of using cookie storing to client's browser coz it might be harmful for your privacy of your data...

thanks,

Kalpan
Avatar of rpkhare

ASKER

Thanks