rpkhare
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>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
@kalmax:
It can be handled with a cookie also? Which one is more faster, session or cookie?
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
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
ASKER
Thanks
<form method="post">
<select name="selname">
<?php
$options = getOptionsFromDatabase();
foreach ($options as $option)
{
echo '<option '.($_POST['selname']==$opt
}
?>
</select>
...
</form>