Retain option value from select list

I have tried all I know, but can’t seem to figure out how to re-display a selected option value from an option list once I have returned to the form from a validation script. All other text fields in the form are retaining their values through the use of SESSION variables which have been populated from the POST variables submitted by the form. However, the drop down list for the state portion of the user’s address reverts to an unselected value.

My code looks like this:

** State: <?php require_once ("Includes/"); foreach ($stinfo as $stname=>$st) { ?> <?php echo ($stname); ?> <?php } ?> //this populates a list of state names to select from //Session variable is set in validation page <?php if(isset ($_SESSION['UserState'])) { //set selected option value $selst=$_SESSION['UserState']; echo ''. $stname .''; }?> [/php]

I have been database programming for several years, but this is my first attempt at PHP so any suggestions or comments will be most welcome.

Try this :slight_smile:


<?php require_once ("Includes/"); foreach ($stinfo as $stname=>$st) { echo ''.$stname.''."\n"; } ?> [/php]

Worked perfectly!!! Thanks so much.

I understand everything about your code except the final part. What part does the ."\n" play in the solution?

Thanks again

This is “new line”, just to make generated html code readable. You can just remove it.

Sponsor our Newsletter | Privacy Policy | Terms of Service