Page 1 of 1

keep selected item in Select after post

Posted: Sun Nov 21, 2010 6:16 am
by mehargags
hi all,
my first post here!!! learning to be a PHP programmer now!!!

I've made 3 date selection SELECT boxes displaying month/day/year but I'm not able to return the previously selected item after POST..

My code

Code: Select all

<?php



echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
	$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December');
$weekday = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$days = range (1, 31);
$years = range (2005, 2015);


//**********************************************

echo "Month: <select name='month' id='month'>";
foreach ($months as $value) {
	echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select><br />';

echo "Day: <select name='day' id='day'>";
foreach ($days as $value) {
	echo '<option value="'.$value.'">'.$value.'</option>\n';
} echo '</select><br />';

echo "Year: <select name='year' id='year'>";
foreach ($years as $value) {
	echo '<option value="'.$value.'">'.$value.'</option>\n';
} 
echo '</select><input type="submit" /></form>';

echo $_POST['month']."/".$_POST['day']."/".$_POST['year'];
?>
I will have some validation on the date afterwards, for which I need to keep the selected items after sumbit so that user doesn't have to reselect his date if it fails validation... Pls hint me where & how "Selected" Tag will be inserted in these dynamically created drop down boxes..

Regards.

Re: keep selected item in Select after post

Posted: Sun Nov 21, 2010 12:02 pm
by s992
Replace the lines where you echo each of the options with this:

Code: Select all

echo (in_array($value,$_POST)) ? '<option value="'.$value.'" selected>'.$value.'</option>\n' : '<option value="'.$value.'">'.$value.'</option>\n';
This checks if the value is in $_POST and then adds the "selected" attribute to the option tag if it is.

Re: keep selected item in Select after post

Posted: Wed Nov 24, 2010 2:37 pm
by mehargags
this works mate!!!
but there is a small problem in managing the POST['Value'] of these elements.

in my form, I have more than one of these drop boxes, & I want incase the php validation fails on other fields, the respective selected values should already be checked...
instead the strange thing happening is that all these dynamic drop boxes take the highest value selected on any of the one...

I'm including the code below.. pls see & Advice

Code: Select all

<?php

echo '
<form action="'.$_SERVER['PHP_SELF'].'" method="POST">';
$days = range (1, 31);
$years = range (1, 31);


//*******  Display the Drop Down Boxes  ***************************************



echo "Day: <select name='day' id='day'>";
foreach ($days as $value2) {
	echo (in_array($value2,$_POST)) ? '<option value="'.$value2.'" selected>'.$value2.'</option>\n' : '<option value="'.$value2.'">'.$value2.'</option>\n';
} echo '</select><br /><br /><br />';

echo "Year: <select name='year' id='year'>";
foreach ($years as $value3) {
	echo (in_array($value3,$_POST)) ? '<option value="'.$value3.'" selected>'.$value3.'</option>\n' : '<option value="'.$value3.'">'.$value3.'</option>\n';
} 
//echo '</select><input type="submit" /></form>';

?>