0

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>
8
Contributors
14
Replies
19
Views
6 Years
Discussion Span
Last Post by karthik_ppts
0
<?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>

Edited by pritaeas: n/a

0

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>
1

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>
2

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

Edited by madCoder: fix icode tag.

Votes + Comments
0

@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).

0
<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>
0

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>";
  }
?>
0
<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>

Edited by karthik_ppts: n/a

0
<select nmae="post">
<option value="1" selected="selected">Home</option>
<option value="2">Contact</option>
<option value="3">Services</option>
</select>

Edited by Ezzaral: Snipped blog promotion link and added code tags.

0

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>
0

$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.

0

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>
0

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

Edited by almostbob: n/a

0

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>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.