Hi all,
Im learning PHP recently so a bit of a noob here...

I'm making a purely php form(without using JS for the moment) which has 2 date selection SELECT boxes displaying month/day

Pls note that I need to be able to return the previously selected Value of these Select Boxes after POST(if the validation Fails)

I'm making all three of these dynamically but I'm facing a confusing problem to which I seek help..All of these Dynamically created drop boxes take the highest value selected on any of the one after post...
somekind of mixing of variable or POST vars is going on

<?php

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


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

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

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

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

?>

can u pls Advice

Recommended Answers

All 2 Replies

Hi mehargags... If you like to retain the last selected date means.., your code is wrong... Try the below shown code.. It works perfectly,...

<?php

@$date=$_REQUEST['date'];
@$month=$_REQUEST['month'];
@$year=$_REQUEST['year'];

echo "<form action='' method='post' name='sample'>";
echo "Date : ";
echo "<select name='date'>";
for($i=1;$i<=31;$i++)
{
echo "<option value=$i ";
if($date==$i)
{
echo "selected";
}
echo ">".$i."</option>";
}
echo "</select>";

echo "<select name='month'>";
for($i=1;$i<=12;$i++)
{
echo "<option value=$i ";
if($month==$i)
{
echo "selected";
}
echo ">".$i."</option>";
}
echo "</select>";

echo "<select name='year'>";
for($i=2010;$i<=2015;$i++)
{
echo "<option value=$i ";
if($year==$i)
{
echo "selected";
}
echo ">".$i."</option>";
}
echo "</select>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
?>

Hope you have fulfilled with this.. If still probs, pls let me know...

works Perfect DragonBaki!!!!

I'm overwhelmed with the quick response in this community, I sincerely hope to become a niche PHP dev with help of DaniWeb guyz.....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.