Hi,I want to retain the selected value of dropdown list during validation check.
Need suggestion.

<select name="year_nam" id="year_nam">
<?php
for($yr_nam=1950; $yr_nam<=2020; $yr_nam++)
echo "<option value=\"$yr_nam\">$yr_nam</option>";
?>
</select>
<?php 
  $year = 2011; 
?>
<select name="year_nam" id="year_nam">
<?php
  for ($yr_nam = 1950; $yr_nam <= 2020; $yr_nam++) 
  {
    $selected = ($yr_nam == $year) ? "selected='selected'" : "";
    echo "<option value='$yr_nam' $selected>$yr_nam</option>";
  }
?>
</select>

Thanks pritaeas,

I am trying to retain the selected value..
Suppose a user select 2000 from drop down & there is a validation error of phone number,
next time when user fill form again,2000 will automatically selected by drop down

<?php 
  $year = 2011; 
?>
<select name="year_nam" id="year_nam">
<?php
  for ($yr_nam = 1950; $yr_nam <= 2020; $yr_nam++) 
  {
    $selected = ($yr_nam == $year) ? "selected='selected'" : "";
    echo "<option value='$yr_nam' $selected>$yr_nam</option>";
  }
?>
</select>

Try this:

<?php 
  $year = 2011;
  $selectedYear = isset($_POST['year_nam']) ? $_POST['year_nam'] : $year; // If you use GET method use $_GET['year_nam'] instead of $_POST['year_nam'] 
?>
<select name="year_nam" id="year_nam">
<?php
  for ($yr_nam = 1950; $yr_nam <= 2020; $yr_nam++) 
  {
    $selected = ($selectedYear || $yr_nam == $year) ? " selected='selected'" : "";
    echo "<option value='$yr_nam' $selected>$yr_nam</option>";
  }
?>
</select>

A slight correction to Zero13's code. Line 9 will always evaluate to true (except for year 0.) $selected = ($selectedYear == $yr_nam ) ? ' selected="selected"' : "";

commented: mmm +4

@madCoder, you're right. You took me the right back. Thanks.

@jacob21
You want to retain the drop-down value, if there is something wrong in submitting the form/validation failed and after the page reloaded, right ?

$selectedYear = isset($_POST['year_nam']) ? $_POST['year_nam'] : $year; // If you use GET method use $_GET['year_nam'] instead of $_POST['year_nam']

This line would keep the selected value if the form has already submitted otherwise it takes default (2011).

<select name="year_nam" id="year_nam">
    <?php
		for($yr=1950; $yr<=2020; $yr++)
		{
	?>
		<option value="<?php echo $yr; ?>" <?php if($yr==$_POST["year_nam"]) echo "selected"; ?> ><?php echo $yr; ?></option>
    <?php
		}
    ?>
    </select>

Hi,i am getting errors.Need suggestions.

1)its take default value 31 not 1
2)not retaining selected value of drop down.

<?php 
 $day_ini = 1;
 $selected = isset($_POST['day']) ? $_POST['day'] : $day_ini;  
// its working fine i am getting selected value of drop down if i  echo  $selected. 
?>
<select name="day" id="day">
<?php
  for ($day_nam = 1; $day_nam <= 31; $day_nam++) 
  {
    $selected2 = ($selected || $day_nam == $day_ini) ? " selected='selected'" : "";
    echo "<option value='$day_nam' $selected2>$day_nam</option>";
  }
?>
<select name="day" id="day">
    <?php
		for($dr=1; $d<=31; $d++)
		{
	?>
		<option value="<?php echo $d; ?>" <?php if($d==$_POST["day"]) echo "selected"; ?> > <?php echo $d; ?> </option>
    <?php
		}
    ?>
    </select>
<select nmae="post">
<option value="1" selected="selected">Home</option>
<option value="2">Contact</option>
<option value="3">Services</option>
</select>

I have same issue, just mine uses ajax with three drop downs, Country, State, City
It works fine and the page after that does what it has to. User selects data click search and it shows data searched for. BUT when user clicks back the boxes dont hold the data that was there when he started search.

Im new to PHP. here is my code, how can i keep what the user selected before the search.

<select name="country" id="country" onChange="filter_state();">              
							
<?php
		
echo "<option selected>Select...</option>";
		
$sql = "SELECT * FROM tbl_country";
	$result = mysql_query($sql);
	$i=0;
	while($row = mysql_fetch_assoc($result)){
	if($i <= 0){
	$country = $row["id"];
	}
	echo "<option value='".$row['id']."'>".$row['fld_country']."</option>";
	$i++;
	}
?>
</select>

$selected2 = ($selected || $day_nam == $day_ini) ? " selected='selected'" : "";

Replace above with the following:

$selected2 = ($selected == $day_nam) ? " selected='selected'" : "";

And first of my post below.

<select name="year_nam" id="year_nam">
<?php
for ($yr_nam = 1950; $yr_nam <= 2020; $yr_nam++)
{
$selected = ($selectedYear || $yr_nam == $year) ? " selected='selected'" : "";
echo "<option value='$yr_nam' $selected>$yr_nam</option>";
}
?>
</select>

I gotta correct the above with the following:

<select name="year_nam" id="year_nam">
<?php
  for ($yr_nam = 1950; $yr_nam <= 2020; $yr_nam++) 
  {
    $selected = ($selectedYear == $yr_nam) ? " selected='selected'" : "";
    echo "<option value='$yr_nam' $selected>$yr_nam</option>";
  }
?>
</select>

The second expression '$day_nam == $day_ini' and '$yr_nam == $year' are not require. Because '$selectedYear' and '$select' assign the specific value with isset($_POST['year_num']) and isset($_POST['day']) methods respectively.
So, '$day_nam' will retains the last value if the form has some problems. If the form has not already submitted, it'll assign default value, i.e., 1. '$yearSelected' has the same case like so.

How to retain value in simple dropdown list..

<td align="right">Name:&nbsp;&nbsp;
<select  style="float:right;"name="pre">
<option value="mr">mr</option>
<option value="Miss">Miss.</option>
<option value="dr">dr</option>

</select>

I tend to use lockouts on correctly set items

if isset($_post['day']) { echo "<input type='text' name='day'  disabled='disabled' value='$_POST['day']'>"; }
else {
 echo '<select name="day" id="day">';
 for ($day_nam = 1; $day_nam <= 31; $day_nam++)   { echo "<option value='$day_nam'>$day_nam</option>";  }
 echo '</select>';
}

havent tested the code, just an option

How to retain value in simple dropdown list..

<td align="right">Name:&nbsp;&nbsp;
<select  style="float:right;"name="pre">
<option value="mr">mr</option>
<option value="Miss">Miss.</option>
<option value="dr">dr</option>

</select>
<td align="right">Name:&nbsp;&nbsp;
    <select style="float:right;" name="pre">
        <option value="mr" <?php if($_POST["pre"]=="mr") echo "selected"; ?>>mr</option>
        <option value="Miss" <?php if($_POST["pre"]=="Miss") echo "selected"; ?>>Miss.</option>
        <option value="dr" <?php if($_POST["pre"]=="dr") echo "selected"; ?>>dr</option>
    </select>
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.