Hi, this is my option value. June is my Default selected Value, but there is a small issue. When Users select any month like January-2010, when he/she click on submit button.
Page will show again June in Option. But I want which the user selects.

<td>
<label>Billing Month
<span class="small">Like September-2010</span>
</label></td>
<td>
<? $year = date("Y") ;

?>
<select name="b" class="billmonth"/ >
  <option value=<? echo "January-".$year; ?>><? echo "January-".$year; ?></option>
  <option value=<? echo "February-".$year; ?>><? echo "February-".$year; ?></option>
  <option value=<? echo "March-".$year; ?>><? echo "March-".$year; ?></option>
  <option value=<? echo "April-".$year; ?>><? echo "April-".$year; ?></option>
  <option value=<? echo "May-".$year; ?> ><? echo "May-".$year; ?></option>
  <option value=<? echo "June-".$year; ?> selected="selected"><? echo "June-".$year; ?></option>
  <option value=<? echo "July-".$year; ?>><? echo "July-".$year; ?></option>
  <option value=<? echo "August-".$year; ?>><? echo "August-".$year; ?></option>
  <option value=<? echo "September-".$year; ?>><? echo "September-".$year; ?></option>
  <option value=<? echo "October-".$year; ?>><? echo "October-".$year; ?></option>
  <option value=<? echo "Novemebr-".$year; ?>><? echo "Novemebr-".$year; ?></option>
  <option value=<? echo "December-".$year; ?>><? echo "December-".$year; ?></option>
</select>
 </td></tr>

Recommended Answers

Need selected in if else for each option tag.

<td>
<label>Billing Month
<span class="small">Like September-2010</span>
</label></td>
<td>
<? 
	$year = date("Y") ;
	if(isset($_REQUEST['submit']))// if submit btn is pressed
	{
		$b = $_REQUEST['b'];
	}

?>
<select name="b" class="billmonth"/ >
  <option value=<? echo "January-".$year; ?> <?=($b == "January-".$year)?'selected="selected"':'';?> ><? …
Jump to Post

What do you have as your form action?
if you call the same scrip the form rest it self.
The seleced value is in your $_POST
do

<?
var_dump($_POST);
?>
Jump to Post

You should use AJAX to avoid refreshing methods or use PHP to executing itself as SELF to retrieve the selected value.

Jump to Post

All 15 Replies

Need selected in if else for each option tag.

<td>
<label>Billing Month
<span class="small">Like September-2010</span>
</label></td>
<td>
<? 
	$year = date("Y") ;
	if(isset($_REQUEST['submit']))// if submit btn is pressed
	{
		$b = $_REQUEST['b'];
	}

?>
<select name="b" class="billmonth"/ >
  <option value=<? echo "January-".$year; ?> <?=($b == "January-".$year)?'selected="selected"':'';?> ><? echo "January-".$year; ?></option>
  <option value=<? echo "February-".$year; ?> <?=($b == "February-".$year)?'selected="selected"':'';?>><? echo "February-".$year; ?></option>
  <option value=<? echo "March-".$year; ?> <?=($b == "March-".$year)?'selected="selected"':'';?>><? echo "March-".$year; ?></option>
  <option value=<? echo "April-".$year; ?> <?=($b == "April-".$year)?'selected="selected"':'';?>><? echo "April-".$year; ?></option>
  <option value=<? echo "May-".$year; ?> <?=($b == "May-".$year)?'selected="selected"':'';?>><? echo "May-".$year; ?></option>
  <option value=<? echo "June-".$year; ?> <?=(!isset($_REQUEST['b']))?'selected="selected"':'';?>><? echo "June-".$year; ?></option>
  <option value=<? echo "July-".$year; ?> <?=($b == "July-".$year)?'selected="selected"':'';?> ><? echo "July-".$year; ?></option>
  <option value=<? echo "August-".$year; ?> <?=($b == "August-".$year)?'selected="selected"':'';?> ><? echo "August-".$year; ?></option>
  <option value=<? echo "September-".$year; ?> <?=($b == "September-".$year)?'selected="selected"':'';?> ><? echo "September-".$year; ?></option>
  <option value=<? echo "October-".$year; ?> <?=($b == "October-".$year)?'selected="selected"':'';?> ><? echo "October-".$year; ?></option>
  <option value=<? echo "Novemebr-".$year; ?> <?=($b == "Novemebr-".$year)?'selected="selected"':'';?> ><? echo "Novemebr-".$year; ?></option>
  <option value=<? echo "December-".$year; ?> <?=($b == "December-".$year)?'selected="selected"':'';?> ><? echo "December-".$year; ?></option>
</select>
 </td></tr>

This code will help u.

What do you have as your form action?
if you call the same scrip the form rest it self.
The seleced value is in your $_POST
do

<?
var_dump($_POST);
?>

What is the use of this?

<?
var_dump($_POST);
?>

Its show selected=selected when i click on submit

this is my code and action page is also same.
so how I keep last selected option when button pressed.

<form action="eims_north.php" method="post" name="form">

<select name="months" style="border-width: 1px;
    border-style: solid;
    border-color: #61A0CE;
	background-image: url(c.jpg);
    background-repeat: no-repeat;
	background-position: center;
	font-family: Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #333333;
    width: 80px;
    height: 13px;" >
	 <optgroup label="Months" style="background-color:#999999; font-weight:bold">
  <option value="January">January</option>
  <option value="February">February</option>
  <option value="March">March</option>
  <option value="April">April</option>
  <option value="May" selected="selected">May</option>
  <option value="June">June</option>
  <option value="July">July</option>
  <option value="August">August</option>
  <option value="September">September</option>
  <option value="October">October</option>
  <option value="November">November</option>
  <option value="December">December</option>
   </optgroup>
</select>

<select name="years" style="border-width: 1px;
    border-style: solid;
    border-color: #61A0CE;
	background-image: url(c.jpg);
    background-repeat: no-repeat;
	background-position: center;
	font-family: Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #333333;
    width: 60px;
    height: 13px;">
	 <optgroup label="Year" style="background-color:#999999; font-weight:bold">
  <option value="2010" selected="selected">2010</option>
  <option value="2011">2011</option>
  <option value="2012">2012</option>
  <option value="2013">2013</option>
  <option value="2014">2014</option>
  <option value="2015">2015</option>
  <option value="2016">2016</option>
	</optgroup>
</select>

<input name="btn" type="submit" Style="width: 50px;
	height: 15px;
	margin-left:0px;
	cursor: pointer;
	border: none;
	background: transparent url(/images/eimssearch.jpg) no-repeat 0 0;" value="" />
</form>

my form name is months.
I am doing like this but its not working, its selecting February every time

<option value="January" selected=<? echo $std1; ?>>January</option>
  <option value="February" selected=<? echo $std2; ?>>February</option>

and calculating like this

<? 
if(isset($_REQUEST['btn']))// if submit btn is pressed
	{
		
	
$m = $_POST["months"]; 

	if($m == 'January')
		{ $std1='"selected"';}
		else
		{$std1='';}
	if($m == 'February')
		{ $std2='"selected"';}
		else
		{$std2='';}
	}
?>
Member Avatar

You should use AJAX to avoid refreshing methods or use PHP to executing itself as SELF to retrieve the selected value.

I do not know about self , please check my code and guide me

selected=<? echo $std1; ?>

is this right way to write it

I solved it using this code

<option <? echo $std1; ?> value="January" >January</option>
  <option <? echo $std2; ?> value="February" >February</option>

and Std1 and std2 like this

if(isset($_REQUEST['btn']))// if submit btn is pressed
	{
		
	
$m = $_POST["months"]; 

	($m == 'January')? $std1='selected="selected"':$std1='';
	($m == 'February')? $std2='selected="selected"':$std2='';
	($m == 'March')? $std3='selected="selected"':$std3='';
	($m == 'April')? $std4='selected="selected"':$std4='';
	($m == 'May')? $std4='selected="selected"':$std5='';
	($m == 'June')? $std6='selected="selected"':$std6='';
	($m == 'July')? $std7='selected="selected"':$std7='';
	($m == 'August')? $std8='selected="selected"':$std8='';
	($m == 'September')? $std9='selected="selected"':$std9='';
	($m == 'October')? $std10='selected="selected"':$std10='';
	($m == 'November')? $std11='selected="selected"':$std11='';
	($m == 'December')? $std12='selected="selected"':$std12='';
}
Member Avatar

Cool! Great work! Do you post our full code here?

Yea , I have put there . if any one have problem , please share with me

Member Avatar

Is it possible to attach this file sample here? so that I can just execute it by pasting it to my localhost. thanks.

Is it possible to attach this file sample here? so that I can just execute it by pasting it to my localhost. thanks.

<form action="this_page.php" method="post" name="form">
<? 
if(isset($_REQUEST['btn']))// if submit btn is pressed
	{
		
	
$m = $_POST["months"]; 

	($m == 'January')? $std1='selected="selected"':$std1='';
	($m == 'February')? $std2='selected="selected"':$std2='';
	($m == 'March')? $std3='selected="selected"':$std3='';
	($m == 'April')? $std4='selected="selected"':$std4='';
	($m == 'May')? $std4='selected="selected"':$std5='';
	($m == 'June')? $std6='selected="selected"':$std6='';
	($m == 'July')? $std7='selected="selected"':$std7='';
	($m == 'August')? $std8='selected="selected"':$std8='';
	($m == 'September')? $std9='selected="selected"':$std9='';
	($m == 'October')? $std10='selected="selected"':$std10='';
	($m == 'November')? $std11='selected="selected"':$std11='';
	($m == 'December')? $std12='selected="selected"':$std12='';
	
$y = $_POST["years"];
	($y == '2010')? $stdy1='selected="selected"':$stdy1='';	
	($y == '2011')? $stdy2='selected="selected"':$stdy2='';
	($y == '2012')? $stdy3='selected="selected"':$stdy3='';
	($y == '2013')? $stdy4='selected="selected"':$stdy4='';
	($y == '2014')? $stdy5='selected="selected"':$stdy5='';
	($y == '2015')? $stdy6='selected="selected"':$stdy6='';
	($y == '2016')? $stdy7='selected="selected"':$stdy7='';
	}
?>
<select name="months" style="border-width: 1px;
    border-style: solid;
    border-color: #61A0CE;
	background-image: url(c.jpg);
    background-repeat: no-repeat;
	background-position: center;
	font-family: Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #333333;
    width: 80px;
    height: 13px;" >
	 <optgroup label="Months" style="background-color:#999999; font-weight:bold">
  <option <? echo $std1; ?> value="January" >January</option>
  <option <? echo $std2; ?> value="February" >February</option>
  <option <? echo $std3; ?> value="March" >March</option>
  <option <? echo $std4; ?> value="April">April</option>
  <option <? echo $std5; ?> value="May" >May</option>
  <option <? echo $std6; ?> value="June">June</option>
  <option <? echo $std7; ?> value="July">July</option>
  <option <? echo $std8; ?> value="August">August</option>
  <option <? echo $std9; ?> value="September">September</option>
  <option <? echo $std10; ?> value="October">October</option>
  <option <? echo $std11; ?> value="November">November</option>
  <option <? echo $std12; ?> value="December">December</option>
   </optgroup>
</select>

<select name="years" style="border-width: 1px;
    border-style: solid;
    border-color: #61A0CE;
	background-image: url(c.jpg);
    background-repeat: no-repeat;
	background-position: center;
	font-family: Arial, Helvetica, sans-serif;
    font-size: 10px;
    color: #333333;
    width: 60px;
    height: 13px;">
	 <optgroup label="Year" style="background-color:#999999; font-weight:bold">
  <option <? echo $stdy1; ?> value="2010" selected="selected">2010</option>
  <option <? echo $stdy2; ?> value="2011">2011</option>
  <option <? echo $stdy3; ?> value="2012">2012</option>
  <option <? echo $stdy4; ?> value="2013">2013</option>
  <option <? echo $stdy5; ?> value="2014">2014</option>
  <option <? echo $stdy6; ?> value="2015">2015</option>
  <option <? echo $stdy7; ?> value="2016">2016</option>
	</optgroup>
</select>


<input name="btn" type="submit" Style="width: 50px;
	height: 15px;
	margin-left:0px;
	cursor: pointer;
	border: none;
	background: transparent url(/images/eimssearch.jpg) no-repeat 0 0;" value="" />
</form>
Member Avatar

Thanks Ayesha, I will test it later if any I will PM you

Be a part of the DaniWeb community

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