?
Solved

Retain DropDown last selected text after POST.

Posted on 2010-01-09
5
Medium Priority
?
878 Views
Last Modified: 2013-11-19
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
Comment
Question by:rpkhare
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
Kalpan earned 1500 total points
ID: 26272855
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
 
LVL 15

Expert Comment

by:ludofulop
ID: 26272861
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
 
LVL 8

Author Comment

by:rpkhare
ID: 26272902
@kalmax:

It can be handled with a cookie also? Which one is more faster, session or cookie?
0
 
LVL 14

Expert Comment

by:Kalpan
ID: 26272922
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
 
LVL 8

Author Closing Comment

by:rpkhare
ID: 31674976
Thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question