Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 897
  • Last Modified:

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

0
rpkhare
Asked:
rpkhare
  • 2
  • 2
1 Solution
 
KalpanCommented:
use the session_start(); on the first row of the your page...

store the selected value to the $_SESSION['lastSelected'] when user select from the list...


if($_SESSION['lastSelected'] !="")){
<option selected="<?php echo $_SESSION['lastSelected']; ?>"
}else{
<option selected="<?php echo $row[1]; ?>"
}



hope this will help...

thanks.

Kalpan

0
 
ludofulopCommented:
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>
0
 
rpkhareAuthor Commented:
@kalmax:

It can be handled with a cookie also? Which one is more faster, session or cookie?
0
 
KalpanCommented:
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
0
 
rpkhareAuthor Commented:
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now